Get Transform from Gameobject?
hey! i have this public gameobject:
public GameObject Spawnpoint1;
and this line of code
Spawnpoint1 = GameObject.Find("CameraRig").GetComponent<CameraControler>().m_Targets[0];
but m_Targets is a Transform value. so how can i avoid this error:
Cannot implicitly convert type `UnityEngine.Transform' to `UnityEngine.GameObject'
Comment
Best Answer
Answer by corn · Jan 14, 2016 at 02:58 PM
You just need to get the GameObject which this Transform is attached to. If you check the scripting API page for Transform, you can see it has a gameObject variable, inherited from Component.
So you only have to assign your target's GameObject to Spawnpoint1.
Spawnpoint1 = GameObject.Find("CameraRig").GetComponent<CameraControler>().m_Targets[0].gameObject;
Answer by Cherno · Jan 14, 2016 at 02:58 PM
Just add .gameObject to the end of the accessed Transform.
Spawnpoint1 = GameObject.Find("CameraRig").GetComponent<CameraControler>().m_Targets[0].gameObject;
Edit: Curse you! 4 seconds! :P
oh snap, cant I accept two answers? but thank you both :)
Your answer
