Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GijsP · Feb 06, 2014 at 04:31 PM · raycastnullreferenceexception

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Feb 06, 2014 at 04:59 PM 0
Share

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.

avatar image GijsP · Feb 06, 2014 at 07:38 PM 0
Share

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

avatar image Jamora · Feb 06, 2014 at 09:47 PM 0
Share

if GetComponent returns null, it means there isn't a TreeController Component on that GameObject.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

null new collider for raycast grabbing 1 Answer

Can't find the null reference 1 Answer

GetComponent, Object Reference not set to an instance of an object 0 Answers

Raycast causes game to crash when shooting up 1 Answer

Raycast: nullreferenceException error spammed, yet working... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges