C# Scoring Script Not Scoring
Using Unity 5.1.1f1 - I took the scoring parts of this Unity tutorial’s C# scripts and cut out the parts I didn’t need: http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points?playlist=17147
I have the Counter script on an empty game object with a box collider, and the Game Controller placed on an empty game object in the hierarchy. “Score: 0” shows up where I want it on the GUI, but no counting happens OnTriggerEnter. GameController and Player are tagged correctly, box collider is set to trigger, the Score Canvas and (child) GUI Text seem okay (‘Score: 0’ shows up at runtime at least), and the Debug Log error in the Counter script does not appear in the console . Can’t figure why this isn’t working. Any help?
using UnityEngine;
using System.Collections;
public class Counter : MonoBehaviour
{
public int scoreValue;
private GameController gameController;
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (gameObject.tag == "Player")
{
gameController.AddScore (scoreValue);
Destroy (this);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Text scoreText;
private int score;
void Start ()
{
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
}
does your Player have a Rigidbody? because OnTriggerEnter is only sent if one of the Objects has a Rigidbody as mentioned here Therefor no Rigidbody -> No Trigger
Answer by Levithan6785 · Nov 05, 2015 at 06:30 PM
if (gameObject.tag == "Player")
try replacing this line with
if (other.gameObject.tag == "Player")
That should do it.
Answer by OctoMan · Nov 05, 2015 at 06:31 PM
It seems you lose the connection to the gamecontrollerobject:
do this instead and try again:
private GameController gameController;
private GameObject gameControllerObject;
void Start ()
{
gameControllerObject = GameObject.FindWithTag ("GameController");
You don't need the GameObject to gain access to another script. You could just do.
private GameController gameController;
void Awake()
{
gameController = GameObject.FindWithTag("Player").GetComponent<GameController>();
}
Answer by boxmyth · Nov 05, 2015 at 07:02 PM
It was just a case of missing "other" in the gameObject.tag line. Works great now! Thanks to both of you for taking the time!
Your answer
Follow this Question
Related Questions
How can i get a scoring system? 1 Answer
Is there a proper way of clamping from 0 to infinity (max value)? 2 Answers
Error:CS0117 How can do I code booleans with appilicationLoadLevel C# 2 Answers
Anim object with Relative Position 1 Answer
Working on dual controls using character controller 0 Answers