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 Ceegore · Mar 09, 2014 at 06:42 AM · variablegetcomponentaccessing

Help with accessing scoring system variable C# plz.

Hi everyone, Firstly I truly want everyone to know I sincerely apologize for having the ask what may be such a simple question, but I can really use another few sets of eyes to help me with this. Here is my prb. I have a simple game, where objects fall down into a basket at the bottom, which the player controls. One type of object that falls into the basket increases the score, however if the player misses the object it will hit a trigger collider at the bottom which will first destroy the falling GO, and set the GUI score to add up. However I have "special" items that fall and if they are caught inside the basket I want them to reverse the score that is attached to the bottom trigger. I feel very confident that I'm close to having this correct, and the game will run and compile fine, however I get a "Null Reference Exception - Object reference not set to an instance of the object", each time one of my "specials" contacts the trigger inside the basket. Can someone please take a look at this and tell me what I've got going wrong. I swear I've looked all over the place and tried my damnedest to figure this out on my own, however this is one prb that has stumped me honestly for the last 5hrs or so...(and I did have to walk away for awhile in between all that time lol). Thanks everyone so much!

Error comes from line 58 of BasketTriggerControl.

using UnityEngine; using System.Collections;

public class BasketTriggerControl : MonoBehaviour {

 private int myScore = 0;
 private int myScoreToLevel = 0;
 private int myLevelScore = 0;
 public TextMesh myScoreText;
 public TextMesh myLevelText;
 public TextMesh myEggScoreDown;
 private bool canLevel = false;
 private DestroyBottomController dbc;



 void Start()
 {
     dbc = GetComponent<DestroyBottomController>();
 }
 
 void Update(){
         if (canLevel == true)
             {
             UpLevel();
             }
     }

 
 void OnTriggerEnter2D(Collider2D objects)
 {
     if(objects.gameObject.tag == "eggs")
     {
         Destroy(objects.gameObject);
         myScore++;
         myScoreToLevel++;
         myScoreText.text = "Eggs = " + myScore;
     }

     if(objects.gameObject.tag == "chicks")
     {
         Destroy(objects.gameObject);
         dbc.broEggScore--;                          <--ERROR HERE
         //Debug.Log (dbc.broEggScore);
         myEggScoreDown.text = "" + dbc.broEggScore;

     }
 
 }







Script I need to change variable on.

using UnityEngine; using System.Collections;

public class DestroyBottomController : MonoBehaviour {

 public int broEggScore;           <--VAR I'M WORKING WITH
 public TextMesh broEggText;
 
 void OnTriggerEnter2D(Collider2D collisionObject) {

     Destroy (collisionObject.gameObject);
     broEggScore++;                           <-- ADDS SCORE HERE
     broEggText.text = "" + broEggScore;

     }


}

Comment
Add comment · Show 1
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 Ceegore · Mar 09, 2014 at 04:53 PM 0
Share

Thanks for the replay guys. However I'm afraid nether of these solutions work. First the Destroy(gameObject) is in the correct place because I want it to Destroy the "special" GO which is falling...not the basket trigger which the script is attached too.

Next the scripts are indeed on two different GO's and that I believe is where the trouble is, however it does have a reference for the dbc....yet according to $$anonymous$$onoDev it does not have a reference to the "broEggScore" var which is on the BottomController Script. How can I have a reference to the other object, yet the engine cant find the var I need to change on that object? And to ensure that it does have the dbc reference I did has was suggested and created a public GO with I then attached with the other object in the inspector.

Thanks so much guys, I really am grateful for the help.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JNetRocks1316 · Mar 09, 2014 at 07:29 AM

The problem is that you're calling Destroy(gameObject) before your script ends.

When you destroy the gameobject you destroy the script (which is still working) so the script can't finish doing what you want it to do :)

Just move Destroy(objects.gameObject); down to the end of that section, after your debug.Log call and score changes.

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 GameVortex · Mar 09, 2014 at 09:14 AM

At the line whith the error is the only time you use the dbc variable. Which means that the dbc variable has not been assigned properly (dbc is null). You assign it in the start function with the GetConponent function, the GetComponent function gets a component attached to the object you use it from. I don't think your DestroyBottomController script is attached to the same object as the BasketTriggerControl or else the two scripts would trigger at the same time and the bottomcontroller would move with the basket as well. I am assuming that they are actually two separate objects instead. I would suggest making the drc variable public and assign it in the inspector in the editor instead. You could also use the Find function to search for it.

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 Ceegore · Mar 09, 2014 at 05:38 PM

Hey Hey....thanks so much guys. At last I finally found a way to resolve the matter.

I still don't understand why I was getting a null reference on the var in the other script, but in order to get around the prb I simply created a method on the BottomController that would do what I want and then just called a SendMessage in the basketTrigger where I was getting the error. This resolved the issue. I know that it's not the absolute most optimal way to do this, however so far I've got resources to spare at run-time...so I can take this happily. :-D

Thank you guys so much!

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

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

Related Questions

Multiple Cars not working 1 Answer

Accessing a variable within a function.... GetComponent() 2 Answers

Problem with accessing variable form other scripts. 1 Answer

Can't understand generic getComponent for Js 1 Answer

GetComponent from string name? 3 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