- Home /
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
Unity professors Nothing? Is not the answer for this problem?
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!!!
Hello and thanks for the guidance; $$anonymous$$y problem is here: Please follow Code comments
public static void GetReturn$$anonymous$$essage(string $$anonymous$$essage)
{
var ret$$anonymous$$essage = $$anonymous$$essage; // Get $$anonymous$$essage of ClassTwo Get$$anonymous$$essage $$anonymous$$ethod // Working
ClassOne Return$$anonymous$$essage = new ClassOne();
Return$$anonymous$$essage.Show$$anonymous$$essage(ret$$anonymous$$essage); // Return $$anonymous$$essage of ClassOne Show$$anonymous$$essage $$anonymous$$ethod // Working
}
private void Show$$anonymous$$essage(string message)
{
Debug.Log(message); // Get $$anonymous$$essage of GetReturn$$anonymous$$essage $$anonymous$$ethod // Working
// $$anonymous$$y Problem is here
// Not Working
NewText.gameObject.SetActive(true);
NewText.text = message;
}
just edited the answer, and attached the fixed from your source code. Check it.
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.
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612145950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
Toggles/Toggle Groups Question 0 Answers