Static, NON-Static!

Greetings,

The question I am about to ask, is probebly one of the most asked question here already, but since I can’t seem to find anyone whit the answers I am looking for I have to ask myself.

Please note that I have been looking into this alot, before I ended up here. I have read everything I could find about it already, so since it didnt help me I ask you kindly to keep the answers to my question only.

My question is about the difference between these two varibles;

public static string myName = "Unity3D";
public string myName = "Unity3D";

I have one class that I want to make this string inside, and I also want to take this string and call it in another script (edit it, display it, and the list goes on), but here is the thing…

I cant call it if its not static, even though I have read alot of answers about that “- Static has nothing to do whit that, just make it public!”, oh NO! That dont work…

So, I make it static… and end up being able to to everything I want whit it in any of my classes! But…

Lets say that I want to make this varibles;

public static GameObject myGameObject;
public GameObject myGameObject;

The problame here is the same, it needs to be static to be able to use in other classes, but, I cant assign anything in my inspector if I have them in static!

Please note that I know that I can assign gameobject in my script too, but what about other things like GUIStyle?

So, to my big question;

  • why is it like this?
  • How can I acces NON-Static varibles, from another script?
  • and how can I make an object reference, which it is asking for everytime I try to acces something NON-Static? What is Object Reference?

(DONT give me workarounds!! THIS is the big reason I am asking!!)

I hope someone can please help me whit this issue, since this is blocking my progress which is very sad now that I am so close to the finnish line of this project!

Best Regards,
Winsjansen!

As you know, making the variable static allows you to call it with: ClassName.MyStaticVariable. Since there’s only one instance of the variable, its easy for the compiler to find it. When its not static, its not so easy because its now part of a class of which there might be many instances. You need to first reference the instance (the script).

That means the other script must do something like:

public MyScript script = GameObject.Find("myGameObject").GetComponent<MyScript>();
script.myStringVariable = "works";

@Eric5h5’s answer is relevant. You’ll either assign the script instance in the inspector or get a reference to it using one of the many other methods proposed. Once you have that script instance, you can access its non-static public variable.

Yes, you CAN call it if it’s not static. See the docs. I find it hard to believe you didn’t come across that link hundreds of times while searching. :wink: (Pay special attention to the parts regarding GetComponent.) Static means there is only ONE instance per class. It has nothing to do with accessibility. What makes a variable accessible from other scripts is whether it’s public or private. If it’s public, it can be accessed (as per the docs, as above). If private, not. Static is a different thing entirely, and is in addition to public/private, not instead of it.

Look there. I tried to make a fully explained principle about script interaction and static:

http://answers.unity3d.com/questions/246211/Having-scripts-interact.html

EDIT: of the use of static

Consider a class:

public class Dwarf{
  static int diamondInVault;
  string name;
  float workingHours;
  public Dwarf(string n){
    name=n;
    workingHours=0; // Even though it is set to 0 anyway...
  }
  public void SetHours(float h){
    workingHours=h;
  }
  public void AddInVault(int d){
    diamondInVault+=d;
  }
}

Now you create 7 instances of this and have 7 objects of type Dwarf. Our little dwarfs are working hard and collecting diamonds in the mine and after work they put the diamonds in a vault. The vault belongs to all the dwarfs and not to one in particular. When one adds a diamond, all of them are one diamond richer (damn commies…).

One fine day, a terrible accident happens and 3 dwarfs are wiped away in a blow.
After a week, Snow-White replaces them with 3 new ones and they work just the same. The vault has not lost the diamond collected by the dead dwarfs and the new ones are putting their diamond in the same vault.

One finer day, the 7 of them are blown in a weird accident. The vault is full of diamond and Snow-White just decides not to replace the dwarfs and leave with the vault.
The vault did not disappear with the death of the dwarfs. It is still there.

The diamondInVault is a static var that will exist from the beginning of the game until the end, even though there is no instance of the class, the class itself exists and so does this static variable. That allows Snow-White to leave with a full vault even though all dwarfs are dead.

That also means that if you have many scenes in your game, the variable can be accessed later on from any scene and it still has the same value.

EDIT: I have noticed that the static variable is not declared public, C# makes variables private by default. Only an instance of the Dwarf class can access it.
If the variable is made public, then Snow-White could simply do:

myDiamond = Dwarf.diamondInVault;
Dwarf.diamondInVault = 0;

simply make variable for Gameobject,
add it to it’s place in Inspector and call your other script attached to that gameobject

using getcomponent(scriptname).variablename :slight_smile: