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 brandonhotdo · Jul 26, 2014 at 04:45 PM · javascriptgetcomponentontriggerenteraccessing scripts

Accessing Variable From Other Gameobject

Ok so I was hoping that when these to objects go collide or in my case enter trigger that they roll a dice and which ever is lower it gets deleted! Here my code.

 #pragma strict
 var Dice = 0;
 Dice = Random.Range(1,100);
 private var anotherScript : SunDele;
 
 function OnTriggerEnter(slr : Collider) {
   anotherScript = slr.GetComponent(SunDele);
   if (anotherScript.Dice > Dice) {
     Destroy (gameObject.transform.parent.gameObject);
   }
 }

Now I have put rigedbody on both and enabled trigger enter but I get the error: NullReferenceException: Object reference not set to an instance of an object SunDele.OnTriggerEnter (UnityEngine.Collider slf) (at Assest/SunDele.js:8) However it did do what I wanted it to do (I can see through the editor) but it pause the game and gives me that error. plz help

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 robertbu · Jul 26, 2014 at 04:46 PM 0
Share

I see nothing wrong with your code or your setup. Put a Debug.Log() between line 5 and 6 to see if OnTriggerEnter() is being called.

avatar image brandonhotdo · Jul 26, 2014 at 05:58 PM 0
Share

I put a debug.log(anotherScript); on line 9 and when I it, it does show me the numbers but still also give me errors :( In the scene view it works but that error pause the game . . . and if I unpause then when I export the game it will be buggy

avatar image robertbu · Jul 26, 2014 at 06:08 PM 0
Share

You need to tell us what error on what line. Paste a copy of the error from the console into a comment. I'll bet is is line 8. You would get this error if whatever triggered the collider did not have a 'SunDele' component.

You could modify line 8 to:

  if (anotehrScript != null && anotherScript.Dice > Dice) {  

Another common solution would be to give all of your 'Dice' objects the same tag and then check the tag before going to the trouble of GetComponent().

avatar image brandonhotdo · Jul 26, 2014 at 06:25 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object SunDele.OnTriggerEnter (UnityEngine.Collider slf) (at Assest/SunDele.js:8)

I think the error is becuase of this e.g. cube1 cube 2 cube 3. cube 1 rolls a dice with cube2 and cube 3 rolls a dice with cube1. cube1 gets deleted and then cube3 can't get compent for the dice as cube1 is gone. I think that could be what is causing the error

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SilentSin · Jul 27, 2014 at 02:25 AM

Edit: looks like line numbering is 1 indexed rather than 0 indexed, so the error line would actually be:

 if (anotherScript.Dice > Dice) {

This means that anotherScript is null, which means that there isn't a SunDele script on the collider that entered the trigger. If you change it to:

 if (anotherScript != null && anotherScript.Dice > Dice) {

Then it will do nothing when a collider without the script enters the trigger, and it will do what you want it to when a collider with the script enters.

Also, on this line:

 Destroy (gameObject.transform.parent.gameObject);

The first reference to gameObject is useless, you could just have:

 Destroy (transform.parent.gameObject);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ren]er cube problem 0 Answers

Object pool with "Null Reference Exception" problem 1 Answer

create GUI.Label and then access to it.. 2 Answers

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

grab a variable off of a script of a child of an 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