Setting camera target after I instantiate the gameobject?

Newbie question on syntax...

I have the mainCamera in the game and I have attached the smoothfollow script to it.

I instantiate the gameobject when the game starts dynamically that I want the camera to follow so I can't set the target in the inspector.

After I instantiate the gameobject what is my syntax in javascript to assign the gameobject I just instantiated to the mainCamera's smoothfollow script?

Thanks for any help.

2 Answers

2

Something close to this:

var sf : SmoothFollow = Camera.main.GetComponent(SmoothFollow);
if (sf)
  sf.target = myNewObject.transform;

You could use the function GetComponent and assign the instantiated GameObject to the variable "target" of the script:

var script : SmoothFollow;
script = Camera.main.GetComponent("SmoothFollow");

var prefab : Transform;
var clone : Transform;
clone = Instantiate (prefab, ... );

script.target = clone;

I'm having the same problem as the person who asked the question, and I originally had the prefab set as "var prefab: Gameobject" because when I set it to a transform it would instantiate clones over and over, but when I set it to a GameObject it gives me the error "cannot convert UnityEngine.GameObject to UnityEngine.Transform. Any idea what's wrong with it?

I found a solution to my particular problem. My camera was set to look at the prefab and not the instantiated (clone), so I added var camLookAt : Transform; and: function SetCamLookAt() { camLookAt = GameObject.FindWithTag ("Player").transform; } to run after SpawnPlayer(); Then updated: useCamera.transform.LookAt( camLookAt );