Hey guys I need help for a score broad system.
Hey guys I need help for a score broad system for my school project. My problem is that I have c sharp coding on my ball and once you score the ball the ball will teleport to its original ball position , and that's great, but it will also give both players a point and that's where i'm struggling to fix if you can lend me a hand that would be great.
using UnityEngine; using System.Collections;
public class BallManager : MonoBehaviour {
public int Score1;
public int Score2;
public void OnTriggerEnter(Collider other)
{
// if (other.gameObject.tag -- "Goal1")
{
transform.position = GameObject.Find("BallPosition").transform.position;
// this.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
Score1 += 1;
}
// if (other.gameObject.tag-- "Goal2")
{
transform.position = GameObject.Find("BallPosition").transform.position;
// this.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
Score2 += 1;
}
}
public void Update()
{
GameObject.Find("Score1").GetComponent<TextMesh>().text = Score1 + "";
GameObject.Find("Score2").GetComponent<TextMesh>().text = Score2 + "";
}
}
I'm a little confused - the if conditions are commented out, why would this ever work if no conditions are being checked?
Answer by nuckolls · May 19, 2017 at 04:00 PM
Thank you madskillzHD for the script and the help and yes I had everything tagged correctly.
Answer by madskillzHD · May 19, 2017 at 02:50 AM
@nuckolls Would this help you?
using UnityEngine; using System.Collections;
public class BallManager : MonoBehaviour {
public int Score1;
public int Score2;
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Goal1")
{
this.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
transform.position = GameObject.Find("BallPosition").transform.position;
Score1 += 1;
}
if (other.gameObject.tag == "Goal2")
{
this.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
transform.position = GameObject.Find("BallPosition").transform.position;
Score2 += 1;
}
}
public void Update()
{
GameObject.Find("Score1").GetComponent<TextMesh>().text = Score1 + "";
GameObject.Find("Score2").GetComponent<TextMesh>().text = Score2 + "";
}
}
Other checks to make sure it should work:
Are your object names the same as in your script?
Are you putting this script in the ball itself?
Have you "Tagged" your objects correctly?
Your answer
Follow this Question
Related Questions
C# - JS problem. 1 Answer
Unity job is failed error. 0 Answers
Only the last clone of my enemies can harm me, they all have the same script 0 Answers
Instantiate after loading scene 0 Answers
How can I reset the score counter? 2 Answers