C# sending from other scripts and sow model ?

Hi …

sorry for this question …

I’m coding by Java for a long time . but I’d like to try C# coding.

so I made a mini example (sending a Void from script to other and show an object)

my problem …
when the second script receive the “LoadModal” Void will do "

  • print (“a”);
  • ShowModal = true;

then will stop :frowning:

  • SpiderMan.SetActive(true); // not working
  • print (“b”); // not working

Note :
when press on the Button “SpiderMan” will show the model .

First Script

private ObjectManager _object = new ObjectManager(); 	
void SendObject(string modal )
{
    _object.LoadModal(modal);
}

Second Script

public GameObject SpiderMan;
public void LoadModal(string modal)
{
	if (modal == "SpiderMan") 
	{
		print ("a");
  		ShowModal = true;
		SpiderMan.SetActive(true);
		print ("b");
	}
}

void OnGUI()
{
	if ( ShowModal ) 
	{
		if (GUI.Button (new Rect (0, 0,100, 50), "SpiderMan")) 
			SpiderMan.SetActive(true);
	}

}

please where is my wrong ?

Thanks…

If ObjectManager derives from MonoBehaviour, then you can’t initialize it using constructor. You have to get existing ObjectManager instance by using GetComponent method. Instead of:

private ObjectManager _object = new ObjectManager();

use this:

private ObjectManager _object;

void Start() // or Awake()
{
    _object = gameObject.GetComponent<ObjectManager>(); // I assume both scripts are attached to the same game object; if not, you have to find proper game object first, and then call GetComponent on it
}