Change a string variable between scripts

Hi.
I need a variable of type string to change when there is a trigger and at the same time, this variable is modified in another script attached to another object.
I use GetComponent and that things, but it does not work and I do not why. Unity gives to me an error (NullReferenceException: Object reference not set to an instance of an object)
I do not know if I have to add both scripts to an object, but in my project is neccesary 2 objects

Greetings.

Most likely you are hitting the case of how to change a string within a method. I say most likely coz you did not provide your code. So you should start with editing your question with the code you described and then we can most likely help you appropriately.

–

Your question is a perfect storm of the FAQ on removable questions: 1. Very little context 2. Frequently asked question (passing values between scripts) 3. Mentioning a NullReferenceException I think you will get much more out of an hour going through the official unity tutorials than you will fielding questions on this forum.

–

Can you share the full script ?

–

3 Answers

3

The issue is the script needs to know the object that the variable is coming from, since you are not using a static class. The simplest way to do this is to create a GameObject variable public at the top of your script

for example:

#include blahblahblah 

public GameObject myObject;

void whateverMethod(){
    myObject.GetComponent<ScriptYoureTryingToReference>().variableName = "blahblahblah";
}

Then all you need to do is drag the game object from the scene into the inspector field that will have appeared after creating a public game object in your script.

If you drag in inspector, why do you drag as GameObject and not as ScriptYoureTryingToReference? First it will narrow down the possible candidate to drag in, and second it will discard the need for GetComponent call. But all in all, your answer is a shot in the dark since there is no provided code and no way to know what is the real problem.

–

Thanks a lot to both of you @sotirosn and @domportera. I now can continue with my project. Greetings

–

It would suggest you are using incorrect methods to reference and change your boolean. foodGotTrigger.PlayOneShot(foodGot); is foodGotTrigger and audio source component? It's not very clear. Should post all of the code... it's probably how you reference the audio component and boolean.

–

If you use GetComponent directly it is the same as doing this.GetComponent which is the same as doing this.gameObject.GetComponent so it will try to get a component off of the GameObject that this component is already attached to. In other words it will try to find a component sibling if you consider this.gameObject to be its parent.

If there is only a single component of this type in your entire scene, which it sounds like there might be as you were confident that GetComponent should be able to return the correct component from anywhere, you could use GameObject.FindObjectOfType<T>(). Since this is a static function it is not tied to an instance and will thus begin its search on all active loaded objects. You will run into problems however if there are more than one of this component or if you component is not active. For what it seems to me like for what you are trying to do you may want to learn about static variables and then you can share information on a global scope without needing instances of objects.

Do you set time scale to 0? If so, change WaitForSeconds to WaitForSecondsRealtime.

–

Thanks a lot to both of you @sotirosn and @domportera.
I now can continue with my project.

Greetings