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 /
  • Help Room /
avatar image
0
Question by haam · Jun 15, 2016 at 08:23 AM · c#unity 5score system

score system failure

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class score : MonoBehaviour {
 
     public int ballValue;
     public Text scoretext;
     // Use this for initialization
     void Start () {
         ballValue = 0;
     
     }
     
     // Update is called once per frame
     void Update () {
         scoretext.text = ballValue.ToString();
     
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.tag == "bucket")
         {
             ballValue = ballValue + 1;
         }
     }
 
 }
 

ok guys what am i doing wrong over here ,i am a beginner.what i am trying to achieve here is i want my ball to fall down to the bucket and get 1 point or score ,my ball has a actual circle collider and a rigidbody and my bucket has box collider which is a trigger and both of these are prefabs which is being used multiple times in the games just in case if anyone want to know.so can anyone tell me what i a, doing wrong or can someone guide to the right tutorial .thank you

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 haam · Jun 15, 2016 at 09:26 AM 0
Share

(after playing with it for a while i am able to get 1 point it does not increase and i am getting this error) Object reference not set to an instance of an object. and it refers to this line.

 void Update () {
          scoretext.text = ballValue.ToString();
 
      }

2 Replies

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

Answer by Mmmpies · Jun 15, 2016 at 11:06 AM

You need the canvas (or something that isn't instantiated and destroyed) to hold a script that keeps the score, that score int needs to be public. Tag that thing so it's easy to find, lets sat it's the Canvas so tag it with an appropriate name like MainCanvas.

Then in the script attached to the buckets you instantiate.

 private GameObject mainCanvas;
 private ScoreScript myScoreScript;
 
 
 void Start()
 {
     mainCanvas = GameObject.FindWithTag("MainCanvas");
     myScoreScript = mainCanvas.GetComponent<ScoreScript>();
 }

Now when it detects the collision/trigger you can update the score variable in the ScoreScript.

 myScoreScript.score ++;

and get the score script to update the text.

this way all buckets can find the right script and update the score.

I've typed this without access to unity so there may be a few errors but the theory is sound.

Comment
Add comment · Show 12 · 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 haam · Jun 15, 2016 at 05:50 PM 0
Share

do you know of any tutorial which guide me through this if so please share

avatar image haam · Jun 15, 2016 at 06:32 PM 0
Share

lets see if i am doing this correct 1.i created an empty gameobject to hold my score script 2.created a new script to call on that original scorescript and attached it to my bucket

i am lost here can someone guide me to a tutorial mmmpies answer look pretty straight forward but couldnt figrue out .

avatar image Mmmpies haam · Jun 15, 2016 at 06:49 PM 0
Share

It a perfectly valid way to do it but your score script is going to interact with the canvas most so, as long as your not instantiating the Canvas just store it on there.

No matter if that is an empty gameObject or a canvas tag it with a reasonable name. Are you O$$anonymous$$ with using tags?

In the script to hold the score have a public int score.

Then in the Start of the script attached to your bucket do that find followed by GetComponent.

Have you OnTriggerEnter or OnCollisionEnter in the bucket script that calls the score script and adds to the public int score.

Let me know which bit you're stuck with.

EDIT - misread part of your last post. Yes you have a mistake - the score script should be on the empty gameObject or canvas.

The script that calls that is on your bucket and in search finds the gameObject/canvas and stores the save script so it can reference it when your collider gets hit.

Because we find the gameObject or canvas in start this happens as soon as the bucket instantiates so each bucket will find the tag for your object be that an empty or the canvas. Then they all find the score script on that object.

avatar image haam Mmmpies · Jun 15, 2016 at 06:55 PM 0
Share

thanks i think i am asking too much from you ,so anyways i will just play with it for few days and if i still cant figure out ,i will surely ask

Show more comments
avatar image Mmmpies · Jun 15, 2016 at 07:40 PM 0
Share

I'd avoid confusing names like having a GameObject called score and a class called score. Change the GameObject to scoreGO so you know which one you're dealing with but the biggest issue is the trigger.

You have the trigger in the score script not in the callscore script. So callscore never gets to use the reference to the scorescript you stored.

$$anonymous$$ove the trigger from the score script to the callscore script

 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.gameObject.tag == "ball")
     {
         scorescript.scoreValue = scorescript.scoreValue +1;
     }
 }
avatar image haam Mmmpies · Jun 15, 2016 at 07:50 PM 0
Share

it worked i cant thank you enough , i was stuck for two days and you saved so much of my time. thank you so much for given that personal touch .

avatar image Mmmpies haam · Jun 15, 2016 at 07:53 PM 0
Share

\o/ \o/

Glad it worked, it's a tough one to get your head around but you will understand it all eventually.

Show more comments
avatar image Mmmpies · Jun 16, 2016 at 08:56 AM 0
Share

Really this question is messy enough already, I'd ask a new one. Ultimately you've managed to pass back the information to the score so your initial question is answered. It'll be easier for you to find the right answer in a few months if you keep your questions and answers concise.

Gut instinct is you have more than 1 script attached to your prefab but if that isn't the fix ask another question specifically about the increment.

avatar image haam Mmmpies · Jun 16, 2016 at 09:36 AM 0
Share

ya its pretty messy

avatar image
0

Answer by Morgenstern_1 · Jun 15, 2016 at 10:37 AM

Are you only using 2D colliders or are you mixing 2D and 3D colliders?

Also the null ref could be caused by you not linking scoretext to a TextField in the inspector.

Comment
Add comment · Show 1 · 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 haam · Jun 15, 2016 at 10:42 AM 0
Share

i am actually using only 2d colliders. but the this is i have linked to textfield that what bugging me ,is it anything to do with bucket being a prefab

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

184 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 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 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 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 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 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 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 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 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

Highscore table C# HELP!!! 0 Answers

Random Generation (Dungeon) with improvements in mind. 0 Answers

How to use the results of a dice roll? 2 Answers

How do i Instantiate sub-Points with in Multiple Points?? 0 Answers

OnGUI will not show up? 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