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 Skippy_123 · Jun 29, 2014 at 02:20 AM · initializationgameobject.findconstructors

Call Method of a Gameobj. Component from other script works, but it does not change variables

Hey Guys,

so I have this very strange problem here which I cannot understand.

There are 3 Scripts:

  • MainGame : MonoBehaviour - put on a gameobject on the scene, this exists first

  • Brain - gets created by MainGame.

  • Gui - gets created by Brain.

When I call a a function from Gui to change a value of MainGame, it doesn't change the variable, but it does run the Debug.Log in the method!

There is only one comment, when I uncomment, the variable readyToGui becomes true, else it stays false all the time.

Here are the scripts:

MainGame.cs

 using UnityEngine;
 using System.Collections;
 
 public class MainGame : MonoBehaviour {
 
     private Brain brain;
     public bool readyToGui;
     
     void Start()
     {
         Debug.Log ("MainGame.Start()");
         this.brain = new Brain();
         readyToGui = false;
     }
     
     public Brain getBrain() { Debug.Log ("MainGame.getBrain();"); return brain; }
     
     public void setReadyToGui() { Debug.Log("set this.readyToGui to true"); this.readyToGui = true; }
     
     public void saySomething() { Debug.Log("MainGame says something"); }
     
     void OnGUI()
     {
         Debug.Log (this.readyToGui);
 
         //this.brain.getGui().initialize();
         
         if(readyToGui) {
             brain.getGui().OnGui(); }
     }
 }




Brain.cs

 using UnityEngine;
 using System.Collections;
 
 public class Brain {
     
     private Gui gui;
     
     public Brain()
     {
         this.gui = new Gui(this);
     }
     
     public Gui getGui() { return this.gui; }
 }


Gui.cs

 using UnityEngine;
 using System.Collections;
 
 public class Gui {
     
     private Brain brain;
     
     public Gui (Brain brain)
     {
         this.brain = brain;
         initialize();
     }
     
     public void initialize()
     {
         GameObject _main_game = GameObject.Find("_main_game");
         MainGame mainGame = _main_game.GetComponent<MainGame>();
         mainGame.saySomething();
         mainGame.setReadyToGui();
     }
     
     public void OnGui()
     {
         showHelp();
     }
     
     private void showHelp()
     {
         GUI.Box(new Rect(10, 10, 100, 100), "helpbox");
     }
 }


And here is the output, when I live it like it is (with the comment):

MainGame.start() UnityEngine.Debug:Log(Object) (1)

MainGame says something UnityEngine.Debug:Log(Object) (1)

set this.readyToGui to true UnityEngine.Debug:Log(Object) (1)

False UnityEngine.Debug:Log(Object) (187)

This is driving me crazy, I so much hope someone of you can help me.

Comment
Add comment · Show 4
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 Kiwasi · Jun 29, 2014 at 03:25 AM 0
Share

Not sure if it matters, but have you tried splitting the setReadyToGui() method out into multiple lines? That was the only thing that looks strange to me. But I've been wrong before, C# might support this.

Otherwise I can't see what's happening.

avatar image gjf · Jun 29, 2014 at 12:08 PM 0
Share

that has nothing to do with it. whitespace is there for readability, not for any functionality/performance. more might add an infinitesimally small amount of time to compile, but that's about it.

the code works as written - there's either a misunderstanding about the order of execution with the constructors or the fact that the OP sets the variable back to false and is then surprised by it being, err, false ;)

avatar image Skippy_123 · Jun 29, 2014 at 12:19 PM 0
Share

I did only set it to false once, when the _main_game GameObject is built. I mean, Start() is only called once right?

avatar image gjf · Jun 29, 2014 at 12:28 PM 0
Share

yes, Start() is only called once but you set your bool to false AFTER the constructors for Brain() & Gui() are executed so setting it to true in setReadyToGui() happens BEFORE you set it to false.

2 Replies

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

Answer by Skippy_123 · Jun 29, 2014 at 03:02 PM

(This Answer is for more then just the above sourcecode from my first post. This is about the code seen in the first following picture. For just the answer for the first post, read the post below, from gjf)

I actually found the problem. I did not initialize everything properly. The Solution and the problem are in 2 picutres I made to see it better, I hope I can help someone somewhen with this.

the finally working sourcecode is the following:

MainGame.cs

 using UnityEngine;
 using System.Collections;
 
 public class MainGame : MonoBehaviour {
     
     private Brain brain;
 
     void Start() {
         Debug.Log ("MainGame.Start()");
         this.brain = new Brain();
     }
     
     public Brain getBrain() { Debug.Log ("MainGame.getBrain();"); return this.brain; }
 
     public void saySomething() { Debug.Log("MainGame says something"); }
     
     void OnGUI() {
         if(this.brain.initialized) {
             brain.getGui().OnGui(); }
     }
 }


Brain.cs

 using UnityEngine;
 using System.Collections;
 
 public class Brain {
     
     private Gui gui;
     public bool initialized;
 
     public Brain() {
         this.gui = new Gui(this);
         this.initialized = false;
 
         if(this.gui.initialized)
             this.initialized = true;
     }
 
     public void saySomething() { Debug.Log("Brain says something"); }
     
     public Gui getGui() { return this.gui; }
 }


Gui.cs

 using UnityEngine;
 using System.Collections;
 
 public class Gui {
     
     private Brain brain;
     public bool initialized;
     
     public Gui (Brain brain) {
         this.brain = brain;
         this.initialized = false;
         initialize();
     }
     
     public void initialize() {
         this.initialized = true;
     }
 
     public void accessBrain() {
         GameObject _main_game = GameObject.Find("_main_game");
         MainGame mainGame = _main_game.GetComponent<MainGame>();
         mainGame.getBrain().saySomething();
     }
     
     public void OnGui() {
         showHelp();
         accessBrain();
     }
     
     private void showHelp() {
         GUI.Box(new Rect(10, 10, 100, 100), "helpbox");
     }
 }


The Problem

The Solution idea


unity_proper_init_problem_01.jpg (317.8 kB)
unity_proper_init_solution_01.jpg (101.4 kB)
Comment
Add comment · Show 2 · 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 gjf · Jun 30, 2014 at 08:27 AM 0
Share

your code still has potential problems. it's possible (although unlikely in this case) that the line

 if(this.gui.initialized)

will throw a null exception. it's good practise to check whether the object is null before accessing any of its members.

i.e. the code should look more like:

 if (this.gui != null)
 {
     if (this.gui.initialized)

 ... rest of code here

 }

you should do something similar with these lines:

 $$anonymous$$ainGame mainGame = _main_game.GetComponent<$$anonymous$$ainGame>();
 mainGame.getBrain().saySomething();

avatar image Skippy_123 · Jul 01, 2014 at 12:05 PM 0
Share

Yes, you're right, this is of course not the perfect code yet. I already got into those problems in my real code (this stuff here was of course just a break down of my real code because it is to long). I had to deal with exactley what you said, because there were so many "branches"/situations, so I had to put different kind of checks for initialized objects everywhere.

So always remember, write good, exception handling and much situation covering initialization codes!

I will probably put all initialization processes in the update routine, checking for everybody all the time till they are ready, and then change a fullInit bool somewhere.

Thanks for the comment!

avatar image
0

Answer by gjf · Jun 29, 2014 at 11:58 AM

the scripts do exactly what you've told them to do - maybe you're confused by the order of execution.

add the following into MainGame.cs before you set readyToGui to false and you'll see that it was set to true.

 Debug.Log(string.Format("Setting readyToGui to false, was {0}", (readyToGui ? "true" : "false")));

try removing line 13 in your original script (`readyToGui = false;`)

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 Skippy_123 · Jun 29, 2014 at 03:26 PM 0
Share

Thanks for the answer. For the code I used for this question at start, this was the solution. While waiting for answers, I had more problems which had to deal with proper initialization (order) which I already solved.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

How to make sure custom init methods are called? 1 Answer

OnEnable not called after all Awake and not all OnDisable before OnDestroy? 1 Answer

initialize with object name 1 Answer

Order of GameObject.FindGameObjectsWithTag(string tag) 3 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