Question regarding accessing scripts of other gameObjects

Hello, I am new to the forums but I have been tweaking with Unity since about a couple of months now.
I am more of a .NET, Programming guy and I was fascinated with Unity so dabbling in it and making my own littel projects to learn it.

I have the following question :

I have a camera and another gameObject (lets say a cube).
The cube has a script to auto rotate.
The camera has a GUI script attached to it with a button that says “explore”

On click of the button,
-the cube should come to the center of the screen, still rotating (which it does)

  • and it shuold enable another script that is placed on the cube (MouseOrbit) (this does not happen)

the following is the code for the MouseOrbit that I use :

var target : Transform;
var distance = 5.0;
var xSpeed = 125.0;
var ySpeed = 50.0;
var zSpeed = 50.0;

static var switchScript : boolean;

var xtransform = 0.000001;
var ytransform = 0.000001;
var ztransform = 0.000001;

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

private var a = 0.0;
private var b = 0.0;

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

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

function LateUpdate () {
    if (target  switchScript ) {
	
	var position = Input.GetAxis("Mouse ScrollWheel") * zSpeed * 0.02 + target.position.z;
       transform.position.z = position;
	   if( Input.GetMouseButton(1)) {
        x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
       y += Input.GetAxis("Mouse Y") * ySpeed * 0.02; 
	   
        var rotation = Quaternion.Euler(y, -x, 0);
       
        transform.rotation = rotation;
		}
		
		
		
    }
	
	
	if ( Input.GetAxis("Horizontal") > 0) {
	transform.position.x += xtransform;
	}
	if ( Input.GetAxis("Horizontal") < 0) {
	transform.position.x -= xtransform;
	}
	if ( Input.GetAxis("Vertical") > 0) {
	transform.position.y += ytransform;
	}
	if ( Input.GetAxis("Vertical") < 0) {
	transform.position.y -= ytransform;
	}
}

If you notice, I have a “switchScript” boolean value in there that I am trying to “switch on” by using the following line of code when I click the “explore” button.

On click of the explore button, the following function is called : you can see the line where it says :

otherScript = GetComponent(MouseOrbitWithSwitch); 
otherScript.switchScript = true
function IdAndLabelling() {

var endPoint2 = new Vector3 (0.0,0.0,0.0); 
rotatingCube.transform.position = Vector3.Lerp(startPoint, endPoint2, (Time.time - startTime) / durationOfMove);
otherScript = GetComponent(MouseOrbitWithSwitch); 
otherScript.switchScript = true;
}

The error I get is : NullReferenceException at the place where I am accesing this script. (possibly because the script I am trying to access is on another GameObject (the cube) and the script from where am trying to access is attached to the Main camera.

Any ideas???

I’m having a similar problem, except im trying to set a boolean to be the same as another boolean.

@ JIM Studios : did it work?? I am working on it today and will post the code if I get it figured out… but any help would be great!

My scipt works… I had to put them both on the same GameObject. So I suspect if we have to access the script on any other GameObject, we will have to access tghe gameObject first and then the script. I still dint try that though.

mine still isnt working, i have the script

var projectile : Transform;
var barrel1 : Transform;
var barrel2 : Transform;
var force = 100;
var canFire = boolean;
var turret : Transform;

var fireRate = .01;

function Update () {
	canFire = turret.gameObject.GetComponent("Turret").on;
	
	if (canFire == true) {
		if (Input.GetButton("Fire1")) {
			SendMessage("fire");
		}
	}
}

function fire () {
	var projectile1 = Instantiate(projectile, barrel1.position, barrel1.rotation);
	projectile1.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
	var projectile2 = Instantiate(projectile, barrel2.position, barrel2.rotation);
	projectile2.rigidbody.AddForce(transform.up * force);
	yield WaitForSeconds(fireRate);
}

and

var Player : Transform;
var playerCam : Transform;
var canGetOn : boolean;
static var on : boolean;
var turretPoint : Transform;
var turretCam : Transform;

function Awake () {
	if (Player == null  GameObject.FindWithTag("Player"))
	Player = GameObject.FindWithTag("Player").transform;
	
	if (playerCam == null  GameObject.FindWithTag("MainCamera"))
	playerCam = GameObject.FindWithTag("MainCamera").transform;
	
	turretCam.camera.enabled = false;
}

function Update () {
	if (Vector3.Distance(turretPoint.position, Player.position) > 1) {
		canGetOn = false;
	}
	
	else if (Vector3.Distance(turretPoint.position, Player.position) < 1) {
		canGetOn = true;
	}

	
	if (on == true) {
		Player.position = turretPoint.position;
		Player.GetComponent(CharacterMotor).enabled = false;
		Player.GetComponent(MouseLook).enabled = false;
		playerCam.GetComponent(MouseLook).enabled = false;
		turretCam.camera.enabled = true;
		playerCam.camera.enabled = false;
		transform.GetComponent(MouseLook).sensitivityX = 5;
		
		if (Input.GetButtonDown("Getin")) {
			on = false;
		}
	}
	
	else if (on == false) {
		Player.GetComponent(CharacterMotor).enabled = true;
		Player.GetComponent(MouseLook).enabled = true;
		playerCam.GetComponent(MouseLook).enabled = true;
		turretCam.camera.enabled = false;
		playerCam.camera.enabled = true;
		transform.GetComponent(MouseLook).sensitivityX = 0;
		
			
		if (canGetOn == true) {
			if (Input.GetButtonDown("Getin")) {
				on = true;
			}
		}
	}

}

function OnGUI () {
	if (canGetOn == true  Screen.lockCursor == true  on == false) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 105, 30), "Press E to get on");
	}
	else if (on == true  Screen.lockCursor == true) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 115, 30), "Press E to get off");
	}
}

and it isnt working

are both scripts attached to the same GameObject?

Try this…

in the second script where you have the GUI to switch on or off,

paste the following code :

otherScript = GetComponent(<scriptname>); // get the object and then the script
otherScript.canFire = true;