Unity: running a function of another object outside of the script

  Kiến thức lập trình

Thank you for reading and helping with this, you are awesome! I’m stuck and this is my first time trying to ask for help online.

I have a circle spawner script to create a new circle (primitive enemy) 3 seconds after the original circle has been destroyed (gets destroyed after player shoots it, that part works). The circle enemy object is calling a function in the spawner script setDead(true) which lets the spawner know the circle is dead and to wait 3 seconds then spawn a new one.


class Circle : MonoBehavior{

public CircleSpawner theSpawner;

Update()

{

theSpawner.setDead(true);

}

}

in unity editor I have dragged the actual spawner that is in the scene into the field for ‘theSpawner’. It calls setDead successfully but it seems to be a different instance of the spawner object, as it reports the isDead bool value to be changed to true but my actual spawner in the game does not change to true it stays false. I even added a random number to a string name value in start() which reports itself fine via debug.log but when the setDead function is called, it also tries to report the random value but this time the value has not been assigned and it shows no number, as if we are talking to a different object, but how is that possible if the reference has been connected to the one and only spawner on the scene?

called the function setDead(bool isDead) from the Circle object referencing the spawner object in the game scene. Expected spawner object to change isDead value to true, but it remains unchanged even though the function call was sucessful.

New contributor

Daniel Lubbersen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT