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 MikeErty · Aug 11, 2013 at 01:05 PM · nullreferenceexceptionlists

Name-fetching script working but with error.

I'm attempting to make a script that controls all the detection of friend and foe for everyone.

I got the code to work as far as adding the object to a list when it enters detection radius and removing it when it leaves. However I will have many variables to pass back and forth, checking the threat level of each enemy etc so the AI can prioritize threats. Because of this I attempted to bring through the names of the characters and add them to a seperate 'name' list.

 var inRangeList : List.< GameObject > = new List.< GameObject >();
 var visibleCloseAgents = new ArrayList();
 
 var agentNameScript : nameGenerator;
 var agentName : String;
 var agentNames : List.<String> = new List.<String>();
 
 var target : GameObject;

 function OnTriggerEnter(other : Collider)
 {
     //Detect character layer agents that enter radius.
     
     //store object that entered
     target = other.gameObject;
     
     //store the object's name generator script so we can take the name
     agentNameScript = other.GetComponent("nameGenerator");
     
     //find the name from the name generator script
     agentName = agentNameScript.characterName;
     
     //add the name to the name list
     agentNames.Add(agentName);
     
     //add the gameObject to the list for storing the game objects
     inRangeList.Add(target);
     
     //reset the target and name vars after adding them to the lists so that we can 
     //add new target and name when a new object enters
     target = null;
     agentName = null;
     
 }

 function OnTriggerExit(other : Collider)
 
 {
     Debug.Log("gone");
     agentNames.Remove(agentName);
     inRangeList.Remove(other.gameObject);
 
 }

The code does in fact pick up the name of the new enemy and stores it as hoped but it fails to remove it from the list when it leaves the radius. Which is annoying as more or less the same code works for the gameObject.

I also get this error which no doubt is a clue as to what's gone wrong but I don't understand. As far as I can see (I'm wrong, I know) the variable does (or did for a moment) exist otherwise I wouldn't have been able to bring through the names.

NullReferenceException: Object reference not set to an instance of an object detectionScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/detectionScript.js:38)

which is this part:

 //find the name from the name generator script
   agentName = agentNameScript.characterName;


I hope someone could shed some light on this problem as I feel close to breaking the back of this vital part of my game. Thank you.

Comment
Add comment · Show 6
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 ArniBoy · Aug 11, 2013 at 01:30 PM 2
Share

I am still new to unity, so I might be wrong. But the problem could be that gameObjects get passed by reference and strings by value, meaning when you set "agentName" to null, the copy in "agentNames" is still intact. using agentNames.remove(agentName);

should work. I would also advice to do the same for the inRange list, as it should still hold a gameObject, at the point where the agentObject was, it just "null" now.

avatar image MikeErty · Aug 11, 2013 at 05:33 PM 0
Share

Thanks for the response. I actually already used that code in the onExit lol! Now, I just added my onTriggerExit code so you can see it. When the object leaves the trigger area I want it to remove all traces of itself from both lists. As it stands the actual target does just that and I'm happy with it. However the names do not. They just stay there.

I know you suggested putting that removal code (agentNames.remove(agentName)) in the onEnter function BUT that would mean it would detect the object and name, add them to lists, then remove them from the lists straight after. But all I want to do in the onEnter is detect the object, store it in a var so we can add that var to a list, THEN clear the var so we can pick up the next thing to put into the list but leave the list entry intact.

Only when the object leaves the trigger do I want it to be obliterated from the lists :)

Just thought I'd mention this in detail in case either of us have misinterpreted the other!

As I've said in response to Spectre it's all working as intended apart from the name removal and the persistent null reference error! Confused!

avatar image ArniBoy · Aug 13, 2013 at 08:24 PM 1
Share

Ah, the removal code obviously belongs into the onExit routine. One question, if you print the agentName you use in onExit to delete the name, what does it say? If it is an empty string, then that is the problem for the faulty removal. $$anonymous$$aybe

  agentNames.Remove(other.GetComponent<nameGenerator>().characterName);

would work then. Using the typseafe GetComponent miiiight also solve the problem you have with the wrong reference, but that would probably be hoping for too much.

avatar image MikeErty · Aug 14, 2013 at 09:51 AM 0
Share

I managed to get rid of the null reference by removing the agentName = null. Presumably that was clearing the variable before it had a chance to be removed. It seems to work now but I still have a few things to iron out (at the moment it struggles with multiple agents: It detects them but on exit it only removes one of them! But that's a question for another time!)

Thanks again for your time.

avatar image ArniBoy · Aug 15, 2013 at 08:21 AM 0
Share

Have you tried putting the line in my last comment ins$$anonymous$$d of

agentNames.Remove(agentName);

in OnTriggerExit? It would solve the problem with multiple enemies leaving I think.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by spectre1989 · Aug 11, 2013 at 01:40 PM

It looks like agentNameScript is null, you can double check this by putting Debug.Log( agentNameScript ) after the call to other.GetComponent( "nameGenerator" ).

If you're sure nameGenerator is the correct spelling, then it seems that the collider doesn't have that script attached to it. This can be the case if for example, you have a GameObject with a nameGenerator script attached to it, and then a few child GameObjects which have the colliders on them. If this is the case, then you can try other.transform.root.GetComponent( "nameGenerator" );

Comment
Add comment · Show 4 · 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 MikeErty · Aug 11, 2013 at 01:54 PM 0
Share

Yes, this is the case. I have the 'human' with a child consisting of a cylinder + collider. They have no scripts. I will try out your suggestion!

avatar image MikeErty · Aug 11, 2013 at 02:59 PM 0
Share

I thought this had solved my problem but sadly I just tested it again and it's back.

As it stands the code picks up the game object and adds it to the objects list fine, takes the name and adds it to the name list fine. It still gives the same error though (despite it working).

Perhaps a clue could be the fact that the name list doesn't delete the agentName when it leaves? The object is removed from the object list but the name stays on the name list and then adds again every time it comes into range. So you have duplicates of the name on the list.

avatar image spectre1989 · Aug 11, 2013 at 03:09 PM 0
Share

Have you tried putting in a Debug.Log statement to see what is returned by GetComponent?

avatar image MikeErty · Aug 11, 2013 at 03:13 PM 0
Share

Apologies, I got so caught up in the last part of your advice that I forgot about the first bit! I just did it and it returned Human (nameGenerator) which sounds right to me...

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

16 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

Related Questions

How to rearange the order of a list after an item was removed? 0 Answers

Unable to delete object from a list using foreach loop 1 Answer

List becomes null in Parent when filled in child script,List becomes null within own class after being filled in other class 0 Answers

Does a new instance of a custom class return null? 2 Answers

Null Ref Exception on a script that should have a reference in it? 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