Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Romano185 · Feb 02, 2012 at 07:08 PM · gameobjectgetcomponentpublic variablefindwithtag

Object reference not set to an instance of an object

Hi everybody

I made a script, which works fine, but it receives a static variable from another script. And because I want to use this script on multiple objects, I wanted to replace those static variables. And I tried it this way:

 var enemyProximity : EnemyProximity = enemy.GetComponent(EnemyProximity);
 var enemy : GameObject = GameObject.FindWithTag("Enemy");
 var destroy : boolean = enemyProximity.destroy;
 
 
 function Update () {
  if(destroy == true){
  Destroy(gameObject);
  }
 }

But Unity displays an error, Object reference not set to an instance of an object, when I use it. How can I fix this problem. There is another script called EnemyProximity, assigned to a different object, which has a boolean variable called destroy, which is set to false when the game starts.

Please help me solve this problem

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mweldon · Feb 02, 2012 at 07:24 PM

I'm not a Javascript guy, but I'd say you need to declare "enemy" before you try to do "enemy.GetComponent".

When you get a script error, it also should tell you what line the error occurred on.

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
avatar image
2

Answer by steakPinball · Feb 03, 2012 at 01:26 AM

You are attempting to use the enemy variable before it's initialized. The key difference is enemy is assigned before calling enemy.GetComponent().

 var enemy : GameObject = GameObject.FindWithTag("Enemy");
 var enemyProximity : EnemyProximity = enemy.GetComponent(EnemyProximity);
 var destroy : boolean = enemyProximity.destroy;
 
 function Update () {
   if(destroy) {
     Destroy(gameObject);
   }
 }

Note: You should consider using Awake or Start to initialize your variables. This is especially important when referencing other objects which may be initializing themselves.

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
avatar image
0

Answer by DeathHawk · Feb 03, 2012 at 01:22 AM

Do you have a GameObject with the tag enemy? And if you do your going to need an array to keep track of the multiple ones since you seem to want this to be dynamic. Also instead of setting the enemy var in creation. I would recommend making it an array and filling the array with all the enemies in the start function. so something like this

var Enemy : GameObject[];

function Start() { //fill list with a loop of your desire. }

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

7 People are following this question.

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

Related Questions

I have a little problem getting a component from another script 1 Answer

Does not contain definition 1 Answer

how to access a float from one script in other script in different objects 3 Answers

enable and disable boxcollider (whats wrong with my script?) 2 Answers

Get the child gameObject of a parent and not the Transform 4 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