- Home /
How to change sprite on a cloned object?
Update 2: I found out that the Animator Controller on the object is "overriding" the sprite. If I disable the animator the correct sprite shows. But then I can't play the animations when they are required. Any ideas???
Update: Check this image library post showing what the sprite is assigned to and how it reverts back: https://imgur.com/a/Fd7RXwd
So I am attempting to change a sprite in the start function of a cloned object. I'm using the code below and when I set the sprite its set to the new sprite, but it's constantly reverting the sprite back to the sprite associated with the prefab object sometime after that. Anyone have an idea how this is possible or what I am doing wrong?
private SpriteRenderer spriteRenderer;
public Sprite break1;
public Sprite break2;
public Sprite break3;
void Start ()
{
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
switch (Type)
{
case DropType.Breakable1:
spriteRenderer.sprite = break1;
break;
case DropType.Breakable2:
spriteRenderer.sprite = break2;
break;
case DropType.Breakable3:
spriteRenderer.sprite = break3;
break;
}
}
I tried an alternate method by setting the sprite directly after instantiating it and this code does not work either:
switch (type)
{
case DropType.Blue:
cs.Drop = Instantiate(Drop1, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Orange:
cs.Drop = Instantiate(Drop2, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Green:
cs.Drop = Instantiate(Drop3, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Red:
cs.Drop = Instantiate(Drop4, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Breakable1:
cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Breakable2:
cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Breakable3:
cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
case DropType.Star:
cs.Drop = Instantiate(Star, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
break;
}
cs.State = CellScript.CellState.Filling;
var ds = cs.Drop.GetComponent<DropScript>();
ds.Type = type;
ds.cellScript = cs;
ds.Fall(ds.transform.position, cs);
var spriteRenderer = cs.Drop.GetComponent<SpriteRenderer>();
switch (type)
{
case DropType.Breakable1:
spriteRenderer.sprite = break1;
break;
case DropType.Breakable2:
spriteRenderer.sprite = break2;
break;
case DropType.Breakable3:
spriteRenderer.sprite = break3;
break;
}
I can see through debugger that sprite is changing from the default prefab, to the new one I am setting. But then it always reverts sometime after this back to what the prefab default.
Answer by tormentoarmagedoom · May 26, 2018 at 09:00 AM
Good day.
Maybe the best way to do is by changing it when you instantiate the object. You can define a GameObjecrt variable, to instantiate an object, so you can modify everything you need from that instantiated object, like any other. Something like this:
GameObject ObjectInstantiated = Instantiate (prefab, position, rotation) as GameObject;
Now The instantiated object is stored in "ObjectInstantiated" variable, so you can edit it as any other obejct in the scene:
ObjectInstantiated.transform.position = NewPosition;
ObjectInstantiated.transform.parent = SomeOtherTransform;
ObjectInstantiated GetComponent<SpriteRenderer>().sprite = break1;
Is a good way to control what and how are you spawning things.
bye!! :D
Hi thanks for your comment. This is basically what I am doing except I had the sprite changing code in the script for the gameobject because I use the game object in multiple other scripts. I tried moving it to the same script I am instantiating the object and it is still not working. It goes back directly to the prefab object sprite.
Is there any other related code? I'd do a search for "sprite" in all code that touches the instantiated object. Then you can make sure some old bit of code, or a mistake, isn't causing the issue.
Hi I thought of that too so I did a search and I'm not accessing the spriterenderer of this object anywhere else in code. I will reply with all relevant code but there really isnt much else.