- Home /
How do I set a target gameobject within another gameobjects component?
Hi, I have this existing code for the other things I need to happen:
void OnMouseDown() {
GameObject.Find("Player_ShotgunDude").GetComponent<PlayerMovement>().enabled=true;
GameObject.Find("Main Camera").transform.position = new Vector3(0, 20, 0);
GameObject.Find("Main Camera").GetComponent<CameraFollow>().enabled=true;
}
I want to add another piece to my OnMouseDown that finds the main camera and its CameraFollow script and changes the target of that script to whatever other gameobject I specified at the time.
I tried looking to see if there was any set target stuff but didnt find anything. Any help would be appreciated.
Cheers.
Answer by Namey5 · Oct 15, 2016 at 10:40 AM
GameObject target;
void OnMouseDown ()
{
Camera.main.GetComponent<CameraFollow>().target = target;
}
Does that not work? It's hard for us to write exactly what would work in your scenario without knowing the other script/elements of your scene.
Your answer

Follow this Question
Related Questions
Removing objects from an array 2 Answers
GetComponent vs AddComponent 3 Answers
C# GetComponent Issue 2 Answers
Acessing a Script Instance from Another Object 3 Answers
What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer