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???