Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ILikeSoups · Sep 07, 2011 at 01:54 AM · parenttagparentingammo

Problem with Ammo Pickup

Ok, so I have this script on my ammo pickup:

 var Ammo : int = 5;
     
     function OnTriggerEnter (theGameObject : Collider) 
     {
         var Gun = GameObject.FindGameObjectsWithTag("spawner");
     
         if(theGameObject.gameObject.tag == "player")
         {
             Gun.gameObject.GetComponent(RayShoot).GetAmmo(Ammo);
         }
             
         Destroy(gameObject);
     }

But when i check for errors in the engine, i get this and it doesn't work:

NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) Ammo Boost1.OnTriggerEnter (UnityEngine.Collider theGameObject) (at Assets/scripts/Powerups/Ammo Boost1.js:9)

I don't understand what the issue is and t can't be the script that is attached to my gun because i used this code previously:

 var Ammo : int = 5;
 var Gun : GameObject;
 
 function OnTriggerEnter (theGameObject : Collider) 
 {
     if(theGameObject.gameObject.tag == "player")
     {
         Gun.gameObject.GetComponent(RayShoot).GetAmmo(Ammo);
     }
         
     Destroy(gameObject);
 }

and it works perfectly, and yes all the tags are in order properly. Could it be because of the fact that the Gun object that the first script is trying to access is under multiple parents?

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

1 Reply

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

Answer by aldonaletto · Sep 07, 2011 at 02:19 AM

In the second script, Gun is the reference to an object you've specified. In the first one, Gun is an array containing all "spawner" objects. You should use it as Gun[0] to get the first "spawner" object. Probably the compiler created a new temporary Gun variable and used it with GetComponent, what caused the error.
You should use FindWithTag instead:

        var Gun = GameObject.FindWithTag("spawner");
and preferably inside Start - but with Gun declared outside the function, like this:

var Gun: GameObject;

function Start(){ Gun = GameObject.FindWithTag("spawner"); }

function OnTriggerEnter( if(theGameObject.gameObject.tag == "player") { Gun.gameObject.GetComponent(RayShoot).GetAmmo(Ammo); } Destroy(gameObject); }

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 ILikeSoups · Sep 07, 2011 at 02:25 AM 0
Share

Great! It works perfectly. Thanks, but what if i want to instantiate the player prefab? Then when i start the scene the variable gun has nothing assigned to it and for some reason when the player is instantiated there is still nothing assigned.

avatar image ILikeSoups · Sep 07, 2011 at 02:34 AM 0
Share

Sorry, never$$anonymous$$d, all i had to do was put the findwithtags in the update function.

avatar image aldonaletto · Sep 07, 2011 at 03:16 AM 0
Share

You should not place it at Update because all Find functions are slow and may affect your frame rate. But you can move this instruction to OnTriggerEnter like before, since it will be called very seldomly.

avatar image ILikeSoups · Sep 07, 2011 at 10:23 PM 0
Share

Okay, thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Parent mesh problem 0 Answers

Set child position and move parents with 2 Answers

Adressing a Child Object after it´s unparented 0 Answers

Enable/Disable all children of parent with tag 1 Answer

Strange movement artifacts when rotating children around parent object 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