- Home /
Null reference exception help
Hello,
I'm trying to work away a null reference exception for a while now, but I can't seem to find what is wrong...
I'm making a script to make the player be able to cut down a tree, I have 3 test trees is my scene with all 3 the same properties, component etc. The only thing that is different is the radius of the capsule collider since it are three different models.
Here are the two scripts, the first one I have attached to my main camera the second one to my trees.
RaycastScript.js (main camera)
#pragma strict
var AxeHitTree : AudioClip;
var TheAnimator : Animator;
var hit : RaycastHit;
function start(){
}
function Update ()
{
//If you have an axe, cut down a tree
if (GameObject.FindWithTag("Axe") != null){
if (Input.GetMouseButtonDown(0) && !TheAnimator.GetCurrentAnimatorStateInfo(0).IsName("Hit1") && !TheAnimator.GetCurrentAnimatorStateInfo(0).IsName("Swing1")){
if (Physics.Raycast(transform.position, transform.forward, hit, 1)) {
if(hit.collider.gameObject.tag=="Tree"){
hit.collider.gameObject.SendMessage("Treehit");
audio.PlayOneShot(AxeHitTree);
}
//chop the fallen tree into blocks
if(hit.collider.gameObject.GetComponent(TreeController).TreeCutDown){
hit.collider.gameObject.SendMessage("ChopBlocks");
}
}
}
}
}
TreeController.js (attached to the trees)
#pragma strict
private var hits : sbyte;
private var t : float;
public var TreeCutDown : boolean;
private var BlockHit: sbyte;
public var Pile_Of_Logs : GameObject;
var FallingTree : AudioClip;
function Awake () {
TreeCutDown = false;
hits = 5;
BlockHit = 5;
}
function Treehit(){
hits --;
if(hits == 0)
{
TreeCutDown = true;
/*
var oldRotation = this.transform.rotation;
this.transform.Rotate(0, 0, 80);
var newRotation = this.transform.rotation;
for (t = 0.0; t <= 1.0; t += Time.deltaTime)
{
this.transform.rotation = Quaternion.Slerp(oldRotation, newRotation, t);
yield;
}
this.transform.rotation = newRotation; // To make it come out at exactly 90 degrees
*/
this.gameObject.AddComponent(Rigidbody);
this.rigidbody.AddForce(0, 0, 3);
audio.PlayOneShot(FallingTree);
}
}
function ChopBlocks(){
BlockHit --;
if(BlockHit == 0){
var TreeLocation : Vector3 = this.transform.position;
Instantiate (Pile_Of_Logs, TreeLocation, Quaternion.identity);
Destroy(gameObject);
}
}
It is worth noting that the commented out code in the TreeController script works and the Rigidbody that I would like to use (since it is not commented out) doesn't...
Also if I double click the null reference exception in the editor it goes to the RayCastScript.js to the line:
if(hit.collider.gameObject.GetComponent(TreeController).TreeCutDown){
I hope someone can help me with this since I'm clueless for whats wrong :(
Thanks in advance,
Gijs
I've made a little guide on null references, you can read it here. Basically anything after a dot can cause a null reference exception. Here you have 4 possibilities, try to figure out which one, for example, by using a Debug.Log.
Well, I knew most of that already, but the question is: Why do I get it, if i look at it, it looks correct, I have the idea that GetComponent is the problem (I checked it via debug.log) but why? The first part works fine until I do:
if(hit.collider.gameObject.GetComponent(TreeController).TreeCutDown){
whatever I do, it won't work...
if GetComponent returns null, it means there isn't a TreeController Component on that GameObject.
Your answer