- Home /
Other
how to change the sprite of instantiated prefabs
I would like to change the sprite during the runtime of a prefab. The sprite renderer is located in a child of the Prefabs 'starC'. Here's what I have done basically:
void Awake() {
starC.transform.parent = transform;
starC.GetComponent<Egg> ().manager = this;
starC.SetActive (false);
starC.name = "starC";
Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
}
void FixedUpdate () {
currentPoolSize = pool.Count;
if (shouldIDraw) {
if (trial == 6) {
shouldIDraw = false;
FinishWithThat ();
}
StartSpawningStars ();
if (changeSprite)
starC.GetComponentInChildren<SpriteRenderer>().sprite = realEgg;
}
else if (!shouldIDraw) {
return;
}
}
but it doesn't change the sprite, mainly because the prefabs was instantiate as a clone of the origincal prefabs and it can't find starC to begin with. Any idea?
Is starC the prefab or is starC an instantiated GameObject?
Probably need to see you StartSpawningStars() function to see what is going on.
Seems suspicious you are setting the sprite from realEgg
in FixedUpdate() but you are loading it into a local variable in Awake().
// This creates a new variable that stores the sprite you load. The variable stops existing as soon as you exit Awake()
Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
...
// Here you set the sprite from a field you declared at the beginning of your class (?) Thisis not the same variable you loaded in Awake
starC.
GetComponentInChildren().sprite = realEgg;
There's nothing wrong with the line with the assignment but perhaps you are not assigning the variable you thought you were.
You need to remove the Sprite
from the beginningof this line in order to store the sprite into the field you use in FixedUpdate()
Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
@aditya007 @leech54 @Nose$$anonymous$$ills sorry I missed a line here: supposed during the Awake() before the transform there's starC = (GameObject)Instantiate (Resources.Load ("Prefabs/EggsInvi"));
where starC is an instantiated prefab
@Nose$$anonymous$$ills apparently making realEgg as a public variable and drag and drop the sprite in inspector works... probably it couldn't find the location of the sprite.
Answer by shadowpuppet · May 19, 2017 at 01:28 PM
I have a sprite change on a prefab in my project but since I know little of coding I take the easy way out. I have BOTH sprites on the prefab and toggle the Sprite renderer On/Off accordingly triggered by a script with a coroutine. My case is an instantiated prefab ( a ragdoll for enemy death) and once instantiated the script runs and 2.3f later the sprite changes
seem like one of the options too. doesn't kill, right. for now it set the sprite variable to public and drag and drop the sprite into inspector. works but not really neat.
Follow this Question
Related Questions
Overwriting a prefab in runtime? 1 Answer
Saving a spheremodel into a prefab 1 Answer
create prefab at runtime 0 Answers
Lighting Mapping the hard way 1 Answer
Instantiate a prefab vs create one dynamically at runtime? 1 Answer