Just getting into C# so I created a simple score system for learning purposes… but when I drag my script onto a cube i get the " must be derived from Monobehaviour " error … but it is !! yes ??
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public int myScore;
void Start (){
myScore = 0;
}
void Update () {
if (gameObject.tag == "scorezone"){
Debug.Log(myScore);
score++ ;
}
}
}
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
private int myScore;
void Start (){
myScore = 0;
}
void Update () {
if (gameObject.tag == "scorezone"){
myScore++ ;
}
}
void OnGUI() { GUI.Box(new Rect(50, 50, 150, 20), "SCORE:" + myScore); }
}
tHis is where I’m upto now… I’m trying to get a ball through a goal. I created a cube (the goal) and added a tag called score zone.
the Gui is not updating … I see it on screen displays a " 0 " I push the ball through but no luck
I am answering your second question (the one you put as answer, which you shouldn’t).
Is that the only script you got? In this part,
void Update () {
if (gameObject.tag == "scorezone"){
myScore++ ;
}
}
Your score will update when the gameObject this script is attached to is tagged as “scorezone”. Furthermore, by this code, no goal is needed to get score; in fact, you can start your script and the score will go up like crazy.
And when you say get a ball through a goal, I think you are thinking about a trigger
.
public class Ball : MonoBehaviour {
public Score scoreObject;
void OnTriggerEnter(Collider other) {
if( other.gameObject.tag == "scorezone" ) {
scoreObject.ScoreGoal();
}
}
}
public class Score : MonoBehaviour {
private int myScore;
void Start (){
myScore = 0;
}
//Add this
public void ScoreGoal() {
myScore++;
}
void OnGUI() {
//Keep whatever you have
}
}
Look up on trigger
for more info.