- Home /
Calling function from other script doesn't change its variables.
Hello, I am currently stuck with this problem.
I have two scripts: IRChandler and Movement. My goal is to call a funtion in Movement via IRChandler, which is done as follows:
IRChandler.cs
public Movement playerControls;
Then, inside unity, the object with the Movement script is dragged into the inspector. Later on in the code, I try calling a function moveUp
from Movement.
if (Input.GetKeyUp("w"))
{
Debug.Log("Moving Up");
playerControls.moveUp();
}
The function looks like this:
Movement.cs
public void moveUp()
{
Debug.Log("Function Accessed");
moving = true;
endpos = transform.position + Vector3.forward;
}
The function gets called, as I get the debug message in the console, but none of the variables (`moving` and endpos
) actually get changed. If I call the same function from the Movement
script, everything works as intended. I have played around with many possible solutions over the past few hours, but I just cant seem to figure it out.
Of course the Variable only gets changed in this void, test this by Debuging the variables in this function. A reason for this is because you can use this as a subroutine to get a result.
Answer by lightzors · Apr 08, 2014 at 02:48 PM
Thanks for the answer. I actually fixed the issue myself by changing the call command to:
if (Input.GetKeyUp("w"))
{
//Debug.Log("Moving Up");
GameObject.FindWithTag("Player1").GetComponent<Movement>().moveUp();
}
it is really weird though, because it seems like the same syntax to me, but this one actually works