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 turbinator · Apr 07, 2013 at 01:49 AM · javascripterrornullreferenceexception

Object reference not set to an instance of an object

When i cut down a tree i get "NullReferenceException: Object reference not set to an instance of an object cutTree.Update ()"

I dont know what i am doing wrong? This is my code:

 var trees: GameObject[];
 var bullitPrefab:Transform;
 
 function Start() {
    trees = GameObject.FindGameObjectsWithTag("Tree"); 
 }
 
 function Update() {
    if(Input.GetButton("placeLog")) {    
       for(var tree in trees) { 
          if(tree == null) 
             continue;

          if(Vector3.Distance(tree.transform.position, transform.position) < 5) 
          {
             Instantiate(bullitPrefab, GameObject.Find("logSpawn").transform.position, Quaternion.Identity);
             Destroy(tree);
             break; 
          }
       }
    }
 }

could i have some help please

Comment
Add comment · Show 4
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 EliteMossy · Apr 07, 2013 at 05:07 AM 1
Share

Don't post multiple questions.

avatar image Loius · Apr 07, 2013 at 06:19 AM 0
Share

$$anonymous$$ultiple questions is fine as long as they're not all the same thing.

But these are. I closed the older one.

avatar image Chronos-L · Apr 07, 2013 at 06:47 AM 1
Share

@vicenti, you might want to know that there are 3 identical questions. Please close this one as well. I have add in the information from that question into this question.

avatar image Chronos-L · Apr 07, 2013 at 06:52 AM 1
Share

@turbinator, I wouldn't eat a horrible-looking but edible food unless I have no choice. Please make your code cleaner and more presentable, those who will answer your question have a choice not to. I have formatted and clean up the codes for you this once.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Cygon4 · Apr 07, 2013 at 10:04 AM

NullReferenceException in general

You already have a check if(tree == null) in there, so I assume you are knowledgeable enough to understand that variables like trees and bullitPrefab can be empty (eg. hold the value null instead of an actual object).

If you try to call a method (`trees.Sort()`) or attempt to access a property (`tree.transform`) on an empty variable, you get a NullReferenceException.

The easiest way to find such issues is by running your script with the MonoDevelop debugger attached because it puts you on the exact line where it occurred (Unity Documentation: Debugger). Attaching the debugger is currently a bit complicated, but knowing how to do it helps with lots of issues.


In your code

For the code you posted, trees could be null if there are no trees in your scene (because GameObject.FindGameObjectsWithTag() returns null instead of a zero-length array if no game objects are found),

The inline call to GameObject.Find("logSpawn") could return null, too, and cause the exception.

Also, bullitPrefab could be null, but I don't believe Instantiate would throw a NullReferenceException when being handed a null prefab.

Comment
Add comment · Share
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

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

14 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

I can do this in JS or not ??? 2 Answers

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

Script not working (Java script) 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