- Home /
Problem when instantiate other gameobject.
Hi im working in a 2dtile based game and im new in unity.
The question is, i have gameobject Enemy with enemy.cs script also i have the prefab gameobject deathSpell with a spell.cs script.
in the top of Enemy class i have this
public GameObject deathSpell; //then i drag the deathSpell to here in inspector
Also i have a function in Enemy script to send a spell to player, like this.
GameObject instance = Instantiate(deathSpell, orgPos, Quaternion.identity) as GameObject;
deathSpell.GetComponent<Spell>().hitTarget(orgPos, tgPos);
Destroy(instance, 3f);
So in the function hitTarget inside Spell class i make this
spellOrg = origemAtk;
spellDest = target;
atack = true;
then in update method of SpellClass i check if atack is true then trace a route to player and hit.
The problem is, the variables of the Spell Class are not being reset even after Destroy command. So if i execute this function
deathSpell.GetComponent<Spell>().hitTarget(orgPos, tgPos);
in the second time not work properly, is looking unity keep script in cache something like this, even if i stop the game and run again.
Answer by hexagonius · Mar 28, 2015 at 02:27 AM
Replace deathSpell with instance. You're referring to the actual prefab, not the instance you just created.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191439im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need Help Push and pull script for co-op/multiplayer 0 Answers
OnTriggerEnter2D doesnt work? 1 Answer
Trying to change character model on mouse down 1 Answer
Simple 2D Enemy AI 3 Answers
how to make 2d tank track? 0 Answers