Hey guys,
I want the GUI buttons I make to rotate the parent of the object I select, and I am having trouble getting the transform of the object I click on using raycast. I have attached my code in the hopes someone has an idea of what I am doing wrong.
The error I keep getting is : (NullReferenceException: Object reference not set to an instance of an object
Select2.OnGUI () (at Assets/Select2.js:24)
//var capsule : Transform;
var clicked : boolean = false;
var hit : RaycastHit;
function Update() {
var capsule : Transform;
if(Input.GetMouseButtonDown(0) &&
collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
Mathf.Infinity)) {
capsule = hit.collider.GetComponent(Transform);
}
}
function OnGUI()
{
var capsule = hit.collider.GetComponent(Transform);
var someText = "Turn Left";
var otherText = "Turn Right";
if (GUI.Button(new Rect(20, 50, 100, 20), someText)){
//capsule = hit.collider.GetComponent(Transform);
capsule.parent.transform.Rotate (0, 0, 45, Space.World);
}
else if (GUI.Button(new Rect(850, 50, 100, 20), otherText)){
//capsule = hit.collider.GetComponent(Transform);
capsule.parent.transform.Rotate (0, 0, -45, Space.World);
}
}