Finding gameobject problem.

Hello, I have a script that is supposed to find the cameras. But, it says that they are null, and it cannot find them. :confused:
Here is my script:

private var camera1 : Camera;
private var camera2 : Camera;
private var camera3 : Camera;
private var camera4 : Camera;
public var startCamera : int = 1;

function Start ()
{
   camera1 = GameObject.Find("Camera_MP4_1").camera;
   camera3 = GameObject.Find("Camera_MP4_3").camera;

   startCamera = 1;
}

function Update ()
{
   if (Input.GetKeyDown ("c")  (startCamera == 1))
   {
      startCamera = 2;
      camera1.enabled = false;
      camera2.enabled = true;
      camera3.enabled = false;
      camera4.enabled = false;
   }
   
   else if (Input.GetKeyDown ("c")  (startCamera == 2))
   {
      startCamera = 3;
      camera1.enabled = false;
      camera2.enabled = false;
      camera3.enabled = true;
      camera4.enabled = false;
   }
   
   else if (Input.GetKeyDown ("c")  (startCamera == 3))
   {
      startCamera = 4;
      camera1.enabled = false;
      camera2.enabled = false;
      camera3.enabled = false;
      camera4.enabled = true;
   }
   
   else if (Input.GetKeyDown ("c")  (startCamera == 4))
   {
      startCamera = 1;
      camera1.enabled = true;
      camera2.enabled = false;
      camera3.enabled = false;
      camera4.enabled = false;
   }
   }

Thanks for any help! :smile:

You only assign camera 1 and 3 from what I see. Try setting all of your camera objects. Also, your method of coding is slightly redundant. You might want to set up an array just do something like

if (input.GetKeyDoen("c")) {
   cameraArray[startCamera].enabled = false;
   ++startCamera;
   if (startCamera > 4)
       startCamera = 1;
   cameraArray[startCamera].enabled = true;
}

hmm. But, the other cameras are attached via the inspector. :confused: But, I’ll try setting them by code.

It works. Thanks! But, this code doesnt find my object that I’m trying to find. Here’s my edited smooth follow script:

private var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")

function Start () {
target = transform.Find("BODY");
}

function LateUpdate () {
	// Early out if we don't have a target
	if (!target)
		return;
	
	// Calculate the current rotation angles
	wantedRotationAngle = target.eulerAngles.y;
	wantedHeight = target.position.y + height;
		
	currentRotationAngle = transform.eulerAngles.y;
	currentHeight = transform.position.y;
	
	// Damp the rotation around the y-axis
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

	// Convert the angle into a rotation
	currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
	transform.position -= currentRotation * Vector3.forward * distance;

	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
}

You can declare public arrays and populate them in the inspector just as easily :slight_smile:

What’s that? Do you have a solution for my other problem?

Bump. I tried GameObject.FindWithTag, but I get an error.

function Start () {
target = GameObject.FindWithTag("Player");
}

The error says: Assets/Scripts/SmoothFollow.js(26,32): BCE0022: Cannot convert ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’.
Any ideas?

if u declare a variable as Transform u have to find a transform,not a GameObject.

change our vatiable
private var target : Transform;
in
private var target : GameObject;

…:face_with_spiral_eyes:

Must those variable really be private ? Can’t you just put them all public, and assign them ?:slight_smile:

Well, I’m instantiating my car, and the cameras are supposed to find the ‘Body’ of the car after it is instantiated. But, they dont find it. :face_with_spiral_eyes:

bump

Bump #2

Just to fix your assignment error:

target = GameObject.FindWithTag(“Player”).transform;

Are you instantiating the car on Start() too?

Chances are that the cameras are looking for the car before it’s actually instantiated… and since it doesn’t yet exist, they get a null instead of a car :stuck_out_tongue:

You should either move the car instantiation to run on Awake(), which will assuredly fire before start(), or move the Find calls to later, perhaps on first update, like:

void Update()
{
    if (!car) car = GameObject.Find("car");
}

Hope this helps

Cheers