How to access private variable in another script?

So lets say in one scene I have two objects with different scripts - script A and script B. And script A has a private variable which i cant make public but REALLY need to access it from script B. Is there any way to do so? I didnt find any answer to this question on forum, all they say is make the variable public which i CANT, or use getcomponent which just gives me an error. Would be very grateful if someone can give me an answer

You should never ever need to access a private field. Normally private fields are private for a reason. But sometimes it’s true that it can be required for a poorly-designed Type.

If you really really really really want to, the term you are looking for is “Reflection”. Just research “how to access a private field through Reflection”, and go on from there. Just beware that this is an anti-pattern and should be avoided

Tysm, actually worked. But can I ask another question? So my game has both visual scripting and c#. Can I access saved variables from visual scripting using c#? I have a saved variable “coins” in visual scripting because for me it was easier to make it in visual script. And now i need to access it in c# script to make shop. Is there any way of doing so? Im sorry but im kinda new to unity, so i dont know much things.

Variables node | Visual Scripting | 1.9.4 (unity3d.com)

Check this article, go to the “Variables API” chapter

Tysm once again. But one last question: how to change a graph variable in c# script (like, add a number to it or something). I was trying to add 20 to a variable but it just said it was an error. Heres how i did that:
Variables.Saved.Set(“Coins”, “Coins” + 20);
but it is an error. Then i tried creating a variable but it didnt work either. Sorry if its obvious, but once agan, im new to c#

Ok it seems you really are new :blush:, so you should practice some more with basic examples on C# so you can get a better grasp!!

Nevertheless, what is happening is that you are adding a “string” (“Coins”) to an integer (20).

The method Variables.Saved.Set("name", value) expects a “value”.
For the value, you are providing "Coins" + 20, on that sentence, “Coins” is just a string, it is not the coins variable. Its like you are doing:

"hello this is just letters" + 20 = ??.

What you want, is to

  • retrieve the Coins value.
  • Add 20 to that value
  • Set the new value to the Coins variable

Variables on “VisualScripting” are not strongly-typed, you should search what that means exactly, but essentially it means that when you Get or Set values, the method is returning or accepting values of type object (all Types inherit from object). So there are “castings” (you should also research what a casting is) involved to be able to get where you want.

It should be something like this, but you should try to study for yourself why this works

int coins = (int)Variables.Saved.Get("Coins");
coints += 20;
Variables.Saved.Set("Coins", coins)

This is assuming, your variable is exactly named “Coins”

1 Like

Yo bro actually TYSM for answering everything.
Im not new to programming, but im new to C# and I suspected that sth is wrong with string, but couldnt figure that out.
Again thank you, you just saved my project :upside_down_face:

1 Like

Yeah…a REALLY last question. So I did it the exact same you showed, but now instead of adding 20, it adds some very big numbers and makes the numbers bigger each time the function activates (yes that add variable is in a function which activates when you press a button) (btw i figured out that coins variable is a float). Any solution?

If it is a “float”, then the casting should have thrown an exception like InvalidCastException: Specified cast is not valid. If it doesnt throw an exception, it might be because you accessed it through c# before ever setting a value from VisualScripting. One way or another, for that you should just be changing “int” to “float” in the previous example (though it wont make much sense as a float unless you need decimal values).

If a very high value is being set, my only guess is that you are either calling the method many times in some way, or modifying the value through VisualScripting somewhere. Try adding a log before and after every place where you might be setting the value so you can see what it is, and debug your way up to the problem.

Anyways, this is already too much off-topic!! try to figure out on your own, and if you still struggle, you can always post a new question (forum posts should be used so other’s can also find solutions, so going off-topic about non-related questions kind of defeats the purpose)

Good luck!

I’m sometimes have the same situation as OP, and I just make the field internal. Does that make sense, or should I make it public?

Internal should only be used if you only want the value to be accessible within in the same assembly (namely if you’re using assembly definitions) to hide implementation details or API’s you don’t want consumers to use.

Otherwise just make it public.

If you want it to be read-only, make a public get-only property pointing to the private field.

2 Likes