Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NerdRageStudios · Nov 17, 2014 at 11:59 AM · static-vars

Advice on static variables and object references

Hi all, just a bit of advice needed here.

I am coding in C# and as my game is growing, I am adding many scripts to the myriad of game objects, but often they need to access scripts assigned to players, enemies etc.

So, simply put I am often re-typing the same code over and over in each script, namely the ones that reference player functionality.

An example would be the players control scripts and animator components.

I am forever having to remember to include the following sorts of code

 // Declaring the var
 
 GameObject player;
 Animator anim;
 
 // Then instantiating
 
 player = GameObject.FindObjectWithTag("Player");
 anim = player.GetComponent<Animator>();
 
 

Then I have to do this for each function, script etc.

Now, I don't mind this, but it seems a little inefficient to me.

So, should I be putting these in one sort of control script that defines these as public statics so that I can reference them in a much simpler fashion?

I have never used statics before, so I am just after advice and opinions please :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richyrich · Nov 17, 2014 at 12:23 PM 1
Share

A static variable is a variable that belongs to the class, not the instance of that class. So for example, if I had a class called Player and in that class I had a variable called count, then to refer to that variable and add '1' I would write something like

Player.count++;

So if count was 0, it would now be one. All objects of the type of your script would see the change, because they all have access to the class data. This is unlike non-static variables, where a change only affects the state of the single object in which the modification occurs and so only the single object would have a change of data value.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Shbli · Nov 17, 2014 at 01:11 PM

You can have a script, that is singleton instead of static, and this way, it's much easier for other scripts to talk to it, and the other way around.

A singleton implemntation is avaiable widely and I highly suggest you read about.

Here is what I do, I have lots of scripts that are singleton (Means, they're available only one, not more) and some of them, I use the DoNotDestroyOnLoad methode, which does make this script not get destroyed when even loading any new level ..

Let's say a GameController (I have also SoundEffectsController, ... etc and properly lots of conrtoller)

The player code can talk to GameController script without using the find methode at all

in my player start methode for example I do this

 //Singleton have a variable called instance, which can call that sinleton and access the controller
 GameController.Instace.player = this.gameObject;
 
 //now on other scripts, they can access the player without doing any GameObject.Find at all
 
 GameController.Instance.player.GetComponenet<Example>().DoThis();
 
 

as well, in my game controller, I tent to save lists of other objects, so I can access them easily, like a list of enemies, a list of NPCs or just anything!

I barely have to use GameObject.Find

and if I ever use the find methode, I usually find myself using transform.FindChild() which finds the child of that transform, not searching through all GameObjects!

Hope this helps.

Singleton for Unity3D C#: http://wiki.unity3d.com/index.php/Singleton

Best of luck!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NerdRageStudios · Nov 17, 2014 at 03:22 PM 0
Share

that's just the kind of thing I was looking for, thanks for taking the time to explain :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why cant I subtract point from my score? 1 Answer

Unityscript: Possible to access static variables dynamically? 1 Answer

static vars in another script not changing? 1 Answer

Disable gameobject at other screen with code ? 0 Answers

How do i use material variables across all scenes? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges