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
(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();
}
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.
do you know of any tutorial which guide me through this if so please 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 .
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.
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
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;
}
}
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 .
\o/ \o/
Glad it worked, it's a tough one to get your head around but you will understand it all eventually.
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.
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.
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
Follow this Question
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