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 /
This question was closed Oct 10, 2013 at 04:51 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by skfs · Oct 09, 2013 at 07:57 PM · gameobjectreferencenull

Null Reference Problem Code+pic (SOLVED)

How can this give me a null reference problem:

 function Update () {
     var AC : AttackComponent = GetComponent(AttackComponent);
     AC.findNearEnemy();
 }


The AttackComponent script is already put on my gameobject which is called "smallUnit":

alt text

Moderator Note: Remember that you can also edit your questions when you need to give more details. I thought the pictures would stay on the server after I deleted the answer. They didn't. -Jamora

EDIT:

First of all...I am so amazed of this community, wow thanks for the replies!

Back to topic: My gameobject has 3 important scripts on it:

  • AttackComponent(instantiated as AC)

  • MoveComponent(instantiated as MC)

  • UnitController(instantiated as UC).

The last stated is the "main" script of the gameobject which works as a bridge between the two first stated, so they can call different functions through the UnitController. Anyway as you can see, the three scripts is attatched to the gameobject:

alt text

Broken down to this scenario, my UnitController has this:

 var AC    :AttackComponent;
 var MC    :MoveComponent;
 
 function Awake(){
     AC    = GetComponent(AttackComponent);
     MC    = GetComponent(MoveComponent);
 }
 
 function Update () {
     AC.findNearEnemy();
 }

My AttackComponent has (broken down for this scenario) this:

     var UC : UnitController;
     
     function Awake(){
         UC = GetComponent(UnitController);
     }
     function findNearEnemy(){
         //nothing yet, just a dummy function         
     }

I think it is a problem with the AC.findNearEnemy(). My MC in UnitController doesn't give me any problems, so I am on unknown ground for my sake :( The error message i get:

alt text

unityhelp.png (78.5 kB)
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by skfs · Oct 10, 2013 at 04:33 PM

I FOUND IT...I FOUND IT.... Sorry for taking anyones time....after some time, I decided to look at the other gameobjects I have in the scene. And here I found, that I have by a mistake applied the UnitController to a gameobject with no such need, and therefore when the script repeatedly called the AC.findEnemyUnit() on an object that didnt have an AttackComponent script on it..it puked the error.. Sorry for anything, and thanks for everything :)

Comment
Add comment · Show 3 · 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 meat5000 ♦ · Oct 10, 2013 at 04:38 PM 1
Share

This brings up an interesting Unity functionality.

Right Click a script in Project Explorer and click

Find References in Scene :P

$$anonymous$$ost useful yet Under-Used!

avatar image skfs · Oct 10, 2013 at 04:51 PM 0
Share

Thank you very much. I will as a fact never forget that again :)

avatar image meat5000 ♦ · Oct 10, 2013 at 04:52 PM 1
Share

Go on :P thumbs up :D

avatar image
1

Answer by meat5000 · Oct 09, 2013 at 07:58 PM

Drag the relevant objects in to the places in the script where you see 'None'

Comment
Add comment · Show 8 · 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 skfs · Oct 09, 2013 at 08:35 PM 0
Share

Oh sorry if you misunderstood me. I have a script called "unitController" this script needs to call findNearEnemy() in another script, which is already on the gameobject, called AttackComponent. So the None spaces should be filled out when game starts :) But i still get a null reference ?

avatar image skfs · Oct 09, 2013 at 08:39 PM 0
Share

The picture is used to illustrate, that i have a script called AttackComponent on my gameobject.

avatar image meat5000 ♦ · Oct 09, 2013 at 10:06 PM 0
Share

Try filling them with dummys. If they get populated anyway it may remove the NullRefs. If not we'll keep debugging.

avatar image robertbu · Oct 10, 2013 at 12:21 AM 0
Share

You will only get a null reference with the code you outline if the current game object does not have an AttackComponent script attached. Are you sure AttachComponent is attached to the same game object as the script above?

avatar image TrickyHandz · Oct 10, 2013 at 03:32 AM 1
Share

As clunk47 stated, please post the full error code. At a glance, I'm guessing the NRE is co$$anonymous$$g from your call to AC.findNearEnemy(). You mentioned that findNearEnemy is controlled by a script called "unitController", and there is a spot in your inspector called "UC" which is a "unitController" that is unassigned. That could be causing your error. Where is the assignment to this "UC" variable being made? Unity won't scan your project or scene for a script to fill your variables for you.

Show more comments

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Null Reference at (wrapper managed-to-native) 0 Answers

Accessing other GameObjects Script Variables 2 Answers

The Variable has not been assigned. 1 Answer

[UNSOLVED] Object reference not set to an instance of an object 0 Answers

Null reference when calling a method from another script 1 Answer


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