how to call an instantiated gameobject?

i instantiated a model in my scene but I want to call the model from a camera script
how can I call an instantiated game object? i have tried to do it trough this … but it reads the error of cant convert Game object to transform. any ideas on how can i achive this?

CODE:

var target:Transform;

var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

@script AddComponentMenu("Camera-Control/Mouse OrbitMP")

function Awake ()
{
if (networkView.isMine == true)
		target =transform.gameObject.Find("marrep_multyplayerReady(Clone)");
	   }
	

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		       
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        
        transform.rotation = rotation;
        transform.position = position;
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

Obviously, the error in your code is that Find returns you a GameObject, where you want your target to be a Transform. I fail to see why you would want a transform instead of a GameObject to be honest.

But if you don’t want to use Find, you could store your instance in a static variable when it’s instantiated.

let’s say you instantiate your model in a script called MyScript, add this in your code:

    static var myInstances:GameObject[]; // a static array of GameObjects
    
    // do some stuff
    
    var newInstance:GameObject = Instantiate(myPrefab, transform.position, transform.rotation);
    myInstances.Push(newInstance);

then later you can reference this array from anywhere using the static reference.

for (var i = 0; i < MyScript.myInstances.length; i++) {
var currentInstance:GameObject = MyScript.myInstances[0];
// and do whatever you want to do to currentInstance
}

edit: Note that I’m using an array here because I don’t know if you plan on instantiating your model several times. If you know you’ll only ever have one single instance, you can use a single GameObject instead of an array.

Oh and btw, static vars don’t appear in inspector, just so you know.

CLOSE, BUT:

you could replace the afformentioned line with:

target =GameObject.Find("marrep_multyplayerReady(Clone)").GetComponent(Transform);
(same as the answer above, that would work..)

To find it by tag, you could say:

target =GameObject.FindWithTag("Some Tag").GetComponent(Transform);

Even more clean (IMHO), you could set the clone as a reference to a variable when you instantiate it, such as (wherever you instantiate it):

var myClone : Transform;

myClone = Instantiate(etc…);

then you can simply refer to it by

target = myClone;

or whatever…

ALSO, since you are using the Awake function, be aware that the object best have ALREADY BEEN instantiated when this script is first created…

If, rather, this script is already on your camera when the target is instantiated, you need to use Update rather than Awake:

function Update(){
if(!target)
target = GameObject.FindWithTag("Some Tag").GetComponent(Transform);
}

(which is the reason it isn’t working)

In awake function :

replace

 target =transform.gameObject.Find("marrep_multyplayerReady(Clone)");

with

 target =GameObject.Find("marrep_multyplayerReady(Clone)").transform;

ok heres an image of whats wrong

alt text