- Home /
When passing a value it becomes null
I have a script called update, this script is given to another script via:
public Update up;
This script is not null as I am successfully able to do:
up.run(1.0f);
I then need to pass up to a script of my player object which i have created through:
cam.SetTarget((Instantiate(player,Vector3.zero,Quaternion.identity) as GameObject));
the script is then put into a variable via:
Player_Movement p = player.GetComponent<Player_Movement>();
and then I try to give the Player_Movement script the Update script via:
p.setUpdate(up);
which simply runs a function in Player_Movement like so: private Update backgroundUpdate; public void setUpdate(Update script){ backgroundUpdate=script; Debug.Log ("set"); } this function is being run successfully, as seen in the log. The problem is that I am then trying to use the run function of the Update script from the player and then I get a null reference exception. The Update was set before I tried to run this.
I don't have much experience with unity but to me it seems like a really obtuse way to do it, as all I am trying to do is set a script in a prefab to a script possessed by a gameObject, however I could not find any way to do it except through such a convoluted manner. However even if this is the wrong way to do this (I am sure it is) I would like to know why I am getting a null reference exception. I apologize if this isn't clear, please do not hesitate to ask any clarifying questions.