Hello, I was wondering how I would see the value of a non-static variable in another script with an if statement and then change the value of the variable in the if statement. I just can’t seem to find a working solution. Thanks, A fellow Programmer
If you use script as attached component to the GameObject, you need first set reference to the that GameObject, in your one of scripts. Then you can access its classes, methods, and values.
If you not sure how to do this things, I advice visit Learn section.
Thanks for the reply however I am not sure exactly where to find this type of information. If you could direct me I would greatly appreciate it.
At the top of your script, reference the script that has the value you want to change.
ScriptToReference theOtherScript;
void Start()
{
theOtherScript = GetComponent<ScriptToReference>();
}
void Update()
{
if(...Whatever condition you set...)
{
theOtherScript.valueNeedingToChange = xyz;
}
}
Something to that effect. Wrote this very quickly before a meeting. Check out the link @Antypodish referenced to learn more.
Thank you very much for the response. I will be trying it in a few hours when I get home.
@Cornysam The code worked! However, I was wondering how I would be able to check the variable in an if statement. Oh and btw I am getting the error Object reference not set to an instance of an object
This is why you should go Through tutorials in learn section. You need build up fundaments first, to catch and understand such nuances.