'Create array GameObject Background
i am trying to create a gameobject array of 4 backgr. Every time you pass the screen, you will change to another backgr. I don't know where I'm going wrong, please help
public GameObject[] Backgrounds;
public SpriteRenderer BakcgrRenderer;
public Vector2 BackgrSize { get => mapRenderer.size; }
public void ChangeBackgr(int? id)
{
if (id == null)
{
return;
}
BackgrRenderer.sprite = Backgrounds[(int)id];
}
Solution 1:[1]
You can use a Dictionary, You can use an ID and a Value, After that you can set a gameobjet from the Dictionary by doing this,
a.Add(Id, Value);
and for getting the value :
GameObject g;
a.TryGetValue(Id, out g);
if (g != null)
{
// code here
}
for this Dictionary I use this :
Dictionary<string, GameObject>
Solution 2:[2]
You are attempting to assign a game Object to the value of a sprite.
Instead of
BackgrRenderer.sprite = Backgrounds[(int)];
You need to access the sprite from the gameObject and assign that
BackgrRenderer.sprite = Backgrounds[(int)].sprite;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Suky Laplante |
| Solution 2 | Jay |
