Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Clofarabine · Jan 24, 2017 at 07:41 AM · objectshierarchy

Does placing things in the hierarchy create objects? How do I reference those objects in code?

I'm a new programmer, coming from Game Maker. I'm having some trouble understanding some OOP concepts in Unity, specifically regarding the hierarchy. Let's say I create an empty called "GameController" and place it in the hierarchy.

 public class GameController : MonoBehaviour
 
 {
 
     public int Score = 50;
     public int PlayerFireMana = 0;
     public int OpponentFireMana = 0;
     public int PlayerFrostMana = 0; 
     public int OpponentFrostMana = 0;
 
 }

I've cut out some of the code to make it shorter, but I'm basically hoping to store the variables of the game that can be affected by many sources in one game controller.

However, when I try to reference this object in another, like:

 void Update (){
 
         if(Input.GetMouseButtonDown(0)){
             GameController.PlayerFrostMana = 10;
 
         }
     }

I get the error "An object reference is required to access non-static member `GameController.PlayerFrostMana'", which I'm assuming is because GameController is a class. I thought putting things in the scene created the actual objects though- could anyone help clear up what I'm misunderstanding? I feel like I'm still missing some important concepts here, but I've been having trouble searching for them.

Thanks!

Comment
Add comment · Show 2
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 Cherno · Jan 24, 2017 at 09:37 AM 0
Share

To access any $$anonymous$$onoBehaviour script, you have to either have a reference to it stored in a variable, or create a static instance of it.

static instance:

 public class GameController : $$anonymous$$onoBehaviour
      
      {
      
          public int Score = 50;
          public int PlayerFire$$anonymous$$ana = 0;
          public int OpponentFire$$anonymous$$ana = 0;
          public int PlayerFrost$$anonymous$$ana = 0; 
          public int OpponentFrost$$anonymous$$ana = 0;
          private static GameController gameController;
          public static GameController instance {
                  get {
             if (!gameController) {
                 gameController= FindObjectOfType(typeof(GameController )) as GameController ;
 
                 if (!gameController) {
                     Debug.LogError("There needs to be one active GameController script on a GameObject in your scene.");
                 }
                 else {
                     gameController.Init();
                 }
             }
 
             return gameController;
         }
         }
 
       void Init() {
         //do whatever you need to do for initializing here
     }
      }


other script

 void Update (){
  
          if(Input.Get$$anonymous$$ouseButtonDown(0)){
              GameController.instance.PlayerFrost$$anonymous$$ana = 10;
  
          }
      }

Reference: Leave the GC script as is.

 public GameController gameController;
 
 void Update (){
  
          if(Input.Get$$anonymous$$ouseButtonDown(0)){
              gameController.PlayerFrost$$anonymous$$ana = 10;
  
          }
      }


avatar image Viiarge · Jan 24, 2017 at 09:57 AM 0
Share

If your GameController is contained in a Game class for example, you can do this :

(This would apply for any component in any $$anonymous$$onoBehavior class)

 public class Game : $$anonymous$$onoBehavior {
 
     private GameController gameController; // Here you declare the instance of the class.
 
     void Start() {
         // Here you find the component in the child.
         gameController = GetComponentInChildren<GameController>(); 

     }
 
     void Update() {
         if(Input.Get$$anonymous$$ouseButtonDown(0)){
              gameController.PlayerFrost$$anonymous$$ana = 10; // That's the code you wanted to do.
          }
     }
 }

1 Reply

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

Answer by Peaj · Jan 24, 2017 at 09:53 AM

By default Unity has multiple ways of accessing other scripts in unity:

You can access scripts/components on the same GameObject via

    this.GetComponent<OtherComponent>()

You can also have public fields of your own types. They are then exposed in the editor and you can drag&drop other GameObjects in there which use your specified script/component. They can then be used like you would use your integer "Score"

Its also possible to FindGameObjectByName() and than go on to find components on the GameObject you just found. (Be aware that names can change. Thus this is not as safe as GetComponent)

There are also mutliple other methods lik GetComponentsInChildren or FindObjectsOfType...

Chernos method is a bit of a "workaround". If you use static instances you should know what you are doing because it can get messy real quick. You should look up UnitySingletons if you are interested in using this approach.

I would also recommend to follow some basic tutorials on the Unity website. You will quickly see how to handle interaction between multiple scripts.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

lost object in hierarchy! connections broken to objects? 0 Answers

DontDestroyOnLoad for hidden objects in hierarchy 1 Answer

How to change object's hierarchy level in animation? 6 Answers

Attach object when NOT in Hierarchy 1 Answer

Hierarchy Class 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