Onuren
December 19, 2013, 4:06pm
1
I have a GameObject, which has a script Script1
as a component.
From another GameObjects GUITextures Script, Script2
, I would like to access Script1.
This isn’t successful:
//Script2.cs
public void OnMouseDown()
{
Debug.Log("Mouse Click");
Script1 _Script1 = (Script1) GetComponent("Script1");
_Script1.DropMarker();
}
When I execute that, GetComponent(“Script1”) returns NULL.
This question comes up frequently. You are closer than most. To get a script from another game object, you need to first find that game object in some way, then get the script from that specific game object. For example:
public void OnMouseDown()
{
Debug.Log("Mouse Click");
Script1 _Script1 = GameObject.Find("GameObjectWithScript1").GetComponent<Script1>();
_Script1.DropMarker();
}
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html