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][1]
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();
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();
ds.Type = type;
ds.cellScript = cs;
ds.Fall(ds.transform.position, cs);
var spriteRenderer = cs.Drop.GetComponent();
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.
[1]: https://imgur.com/a/Fd7RXwd
↧