Share data between scripts with static class ?

Hi,

In my game I’ve several variables I need to share between my scripts.
I don’t like use GetComponent() so I’ve created a static class to store and use them with variables “alias” at beginning of each script, like that :

public class myClass : MonoBehaviour
{
	int foo = Global.Foo;
	// etc...

	//and I use foo in class methods instead of Global.Foo
}

It’s from far the cleanest solution I’ve found.

Any way to do it better ?

yes… ur supposed to use properties : get() set() methods!!!

Example: in Script1.cs
private string foreName;

public string getForeName()
{
return foreName;
}

public string setForeName(String name)
{
foreName = name;
}

Now in the scripts u need to access this string foreName…create a variable of Script1

private Script1 Script1String;

private string name;
//where u need
void whatsName()
{
name = Script1String.getForeName();//gets u the string
//or
Script1String.setForeName(name);//sets the string forename in Script1
}

What you are doing can be done with simply const values, or a parent class you inherit from. Can you provide us a more detailed sample?

From what I can see you dont share any variables between different scripts, you use the “global” variables as simple const values. And this is as far from beeing an alias as Michael J Fox is from beeing a passable Jenga player.

If you want to communicate between different scripts, use GetComponent. Embrace the concept, love it, consume it!
If you want to store some values you want to reuse, use constants.
If you want to have scripts/components with a common ground, use base/parent classes.

@Anusna: C# has a language concept for properties, US too (I think):

public class Foo
{
  public int PropertyA { get; set; } // autogenerated backing fields
  public int PropertyB { get; private set; } // autogenerated backing fields, private setter
  
  private int fieldC = 123;
  public int PropertyC
  {
    get { return fieldC; }
  }

  private fieldD = 0;
  public int PropertyD
  {
    set { fieldD = value; }
  }

  private fieldE = 0;
  public int PropertyE
   {
     get { return fieldE; }
     set { fieldE = value; }
   }
}

uh, it’s not really what I asked but I don’t think there was really a solution, thanks anyway.

What is your goal by defining variables to hide the global static variables?

If it is to shorten the Global name, you could do using G = Global;. This makes it so that you could just type “G.foo = 1” instead of “Global.foo = 1;”.

Using a global or static members of a class is not ideal. You could look at making the component a singleton. And then all you’d have to do is grab the reference to the instance.

How about a pattern like(note not all the code is here):

static T instance;
static <T> GetInstance() {
  if (instance ==null) {
      // Find an instance - this way you can drop an instance in to scene and edit settings using the editor
      instance = GameObject.FindObjectOfType(typeof(T)) as T;
      // Didn't find one then create one (something like: go = new GameObject(); go.AddComponent(typeof(T));)
      if (instance == null) instance = CreateInstance();
  }
  return instance;
}

I use something similar for most of my manager/controller type classes.

Yep. That or check out tip 29 on this page: 50 tips for working with unity best practices

This page has some really good tips on it in general. But 29’s Singleton class is a good base to inherit all your singletons from. Especially if you follow the second part of the tip which suggests to make static accessors for commonly used member variables.

To be honest, it was not even clear what exaclty you wanted as your code made id look like you want to use constants.