Im making pong game, i want to send true or false depending on the wall the ball struck.
the score gets updated the right way but i keep getting this error
units says the error is in the **score** script in the first line of **void funtion**
void Update()
{
scoreR.text = pR.ToString();
scoreL.text = pL.ToString();
}
here is my **ball movement code**
public class BallMovement : MonoBehaviour
{
public float speed = 30f;
Rigidbody2D rb;
int p;
void Start()
{
rb = GetComponent();
rb.velocity = Vector2.right * speed;
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
return (ballPos.y - racketPos.y) / racketHeight;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "LeftRacket")
{
float y = hitFactor(transform.position, `col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(1, y).normalized;
rb.velocity = dir * speed;
}
if (col.gameObject.name == "RightRacket")
{
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(-1, y).normalized;
rb.velocity = dir * speed;
}
if (col.gameObject.CompareTag("Wall"))
{
if (col.gameObject.name == "LeftWall")
{
Score.instance.score(true);
}
else if (col.gameObject.name == "RightWall")
{
Score.instance.score(false);
}
}
}
}
and my **score script**
public class Score : MonoBehaviour
{
public static Score instance;
public TextMeshProUGUI scoreR;
public TextMeshProUGUI scoreL;
int pR = 0;
int pL = 0;
// Start is called before the first frame update
void Start()
{
instance = this;
}
// Update is called once per frame
void Update()
{
scoreR.text = pR.ToString();
scoreL.text = pL.ToString();
}
public void score(bool racket)
{
if (racket == true)
{
pR += 1;
}
else if (racket == false)
{
pL += 1;
}
}
}
Also is there any other way to send the bool from ball movement script to my score script without instance or is this the best way?
↧