• 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
Question by GabrielSpark · Jan 06, 2018 at 08:52 PM · c#uiclasscodepage

GameObject SetActive Component not working After return message

HI every body, I have a trouble, UI Text object SetActive Component Not working after return message from other class This Action working before send message and get message from other class with no problem. But After return message Component Not working!!!

Please Help me for resolve this problem. Thanks

My Class Code is:

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ClassOne : MonoBehaviour
 {
     public Text NewText;
     public Button NewButton;
     
     
     protected void OnAwake()
     {
 
         NewText = NewText ?? transform.FindChild("Message").GetComponent<Text>();
         NewButton = NewButton ?? transform.FindChild("Button").GetComponent<Button>();
 
         NewText.gameObject.SetActive(false);
     }
 
     private void ShowMessage(string message)
     {
         // Working
         Debug.Log(message);
         
         // Not Working
         NewText.gameObject.SetActive(true);
         NewText.text = message;
     }
     
     public static void GetReturnMessage(string Message)
     {
         var retMessage = Message;
         ClassOne ReturnMessage = new ClassOne();
         ReturnMessage.ShowMessage(retMessage);
      }
 
     public void OnButtonClick()
     {
         NewText.gameObject.SetActive(false);
         
         ClassTwo.Instance.GetMessage();
         
     }
 
 }
 

And Class Two is:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ClassTwo : MonoBehaviour
 {
 
     public void GetMessage()
     {    
 
         var message = ("This is Return Message");
 
         ClassOne.GetReturnMessage(message);
 
     }
 
 }
 

Hi buddy, Please check attachment link text

assets.zip (6.1 kB)
Comment

People who like this

0 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 GabrielSpark · Jan 09, 2018 at 01:28 AM 0
Share

Unity professors Nothing? Is not the answer for this problem?

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by JVLVince · Jan 09, 2018 at 02:34 AM

I wonder that you are trying to implement singleton to your code but I think you implement it wrong way. If you have time and interesting in design pattern, take a look here

The other problem is you try to new a Monobehaviour, you can't (shouldn't) do it. With monobehaviour, you should use AddComponent. I suggest you spend some free time on the fundamentals.

  • Here is the most simple way to fix your problem (without any singleton):

In ClassTwo:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class ClassTwo : MonoBehaviour {
 
     public ClassOne m_classOne;
 
     public void GetMessage()
     {    
         var message = ("This is Return Message");
         m_classOne.GetReturnMessage(message);
     }
 }

Then in Unity Editor, drag and drop ClassOne Gameobject to ClassTwo's m_classOne field.

In ClassOne, change GetReturnMessage method to non static method and remove the new ClassOne line:

 public void GetReturnMessage(string Message)
 {
     var retMessage = Message;         
     ShowMessage(retMessage);
 }

  • Here is the fix within your design (I think so) by using singleton:

In ClassOne, change the GetReturnMessage method like above, then add this bunch of code:

 private static ClassOne m_instance;
     public static ClassOne Instance(){
         if (m_instance == null){
             m_instance = GameObject.FindObjectOfType<ClassOne>().GetComponent<ClassOne>();
         }
         return m_instance;
     }

In ClassTwo, instead of call ClassOne.GetReturnMessage(), use ClassOne.Instance.GetReturnMessage().


Edit:

From your provided source, I juts rewrote the code exactly what I wrote on answer (Check here) If you run your own code and see the console window, it'll show exactly what you missed. I suggest you spend more time on how to use console log effectively. I suspected that you even not try with the solution you got from answer... I just provide you the way to fix within your design and you should read carefully what @Bunny83 answered.

Hope this help, Cheers!!!


assets.zip (9.0 kB)
Comment

People who like this

0 Show 7 · 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 GabrielSpark · Jan 09, 2018 at 06:56 AM 0
Share

Hello and thanks for the guidance; My problem is here: Please follow Code comments

      public static void GetReturnMessage(string Message)
      {
          var retMessage = Message; // Get Message of ClassTwo GetMessage Method // Working
          ClassOne ReturnMessage = new ClassOne();
          ReturnMessage.ShowMessage(retMessage);  // Return Message of ClassOne ShowMessage Method // Working
       }
  
      private void ShowMessage(string message)
      {
 
          Debug.Log(message); // Get Message of GetReturnMessage Method // Working
 
          // My Problem is here
          // Not Working
          NewText.gameObject.SetActive(true);
          NewText.text = message;
      }
      
 

avatar image JVLVince GabrielSpark · Jan 10, 2018 at 05:07 AM 0
Share

just edited the answer, and attached the fixed from your source code. Check it.

avatar image JVLVince GabrielSpark · Jan 10, 2018 at 05:11 AM 0
Share

if it solved your problem just mark this as answers.

avatar image GabrielSpark JVLVince · Jan 10, 2018 at 06:51 AM 0
Share

Thanks alot man

Show more comments
Show more comments
avatar image

Answer by Bunny83 · Jan 09, 2018 at 02:15 AM

There are several things wrong here. First of all "ClassOne" is a monobehaviour and therefore a component. Components and MonoBehaviours can not be created with "new". They need to be attached to a gameobject. In line 34 of your ClassOne class you create an instance with "new".


Next thing is Unity only has a callback that is called "Awake", not "OnAwake". So your OnAwake method will never be called.


Finally you should avoid the use of the "null coalescing" operator "??" in Unity. This operator does an actual reference comparison. Since Unity often works with "fake null" objects the operator may not do what you think it should do. For more information see this forum post.


Ps: Your overall setup seems a bit strange. Why do you have two classes which are tightly coupled as the both require each other. Why do you use static methods if you actually need an instance? It's really hard to see any useful pattern here.

Comment
Ginxx009
JVLVince

People who like this

2 Show 0 · 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

Answer by GabrielSpark · Jan 09, 2018 at 06:59 AM

Hello and thanks for the guidance; My problem is here: Please follow Code comments

      public static void GetReturnMessage(string Message)
      {
          var retMessage = Message; // Get Message of ClassTwo GetMessage Method // Working
          ClassOne ReturnMessage = new ClassOne();
          ReturnMessage.ShowMessage(retMessage);  // Return Message of ClassOne ShowMessage Method // Working
       }
  
      private void ShowMessage(string message)
      {
 
          Debug.Log(message); // Get Message of GetReturnMessage Method // Working
 
          // My Problem is here
          // Not Working
          NewText.gameObject.SetActive(true);
          NewText.text = message;
      }
      
 


Attach a simple test of project, Please check and help me!

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

443 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Buttons don't work after Unity 2017 update 0 Answers

Convert Audio Beeps And Dashes into Text 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Calling functions from other namespaces and scripts. 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