Altering ints in one script via another script?

Hi, I’ve been spending the last two days trying to figure this out (and yes, I tried the answers section, I asked several variants of the question and received no answers and I don’t have time to hope someone will dig my question up)

In short, a friend gave me a public function to finish. When the function is called, it defines an int called symbolNum. the script then finds the gameobject with the same number as symbolNum, and then toggles a bool. symbolNum is not defined in initial script though.

A separate script is supposed to give symbolNum its value. Basically, it’s an inventory system that, upon finding an item, will give symbolNum its value, triggering the first script to find and toggle the corresponding item as picked up. Problem is, I have no idea how to do that. I’ve tried using GetComponent, but all I can do is find the entire first script (I’m only working with part of it) and some of the predefined variables in that script. I think it’s because symbolNum is not defined at the beginning of the script, and instead inside the brackets at the beginning of the function. Can anybody help me? I can’t post the code because it apparently becomes a massive wall of some asian characters when I post it here. Thanks in advance!

It would help if you show us the code involved; it’s easier to read the actual code than a description of it (and its intended behaviour.)

Remember to use code tags properly.

Welcome to the forums!

Did you have time to google? Because there’s a variant of the “How do I access a variable in one script from another” question being asked every hour on the hour on this forum.

If it’s defined in a function then it’s a local variable, which means that it will only be visible inside that function and nowhere else.

Local variable and variable scope are fundamental yet simple programming concepts. If you’re not familiar with them then you would probably benefit from spending more time reviewing the basics.

1 Like