- Home /
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!
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;
}
}
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.
}
}
}
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.
Your answer
Follow this Question
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