- Home /
Question by
proskateboarder7 · Jun 07, 2021 at 11:00 PM ·
triggerunity 2dscorecollider 2d
Score only adds 1 then stops counting!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public GameObject point;
// Start is called before the first frame update
void Start()
{
point.SetActive(false);
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "CheckLeft")
{
point.SetActive(true);
Instantiate (point, new Vector3(0,-2,0), Quaternion.identity);
{
if(GameController.instance.isPoint == false && GameController.instance.EndMatch == false )
{
GameController.instance.number_PointsRight++;
GameController.instance.isPoint = true;
}
}
}
if(collision.gameObject.tag == "CheckRight")
{
point.SetActive(true);
Instantiate (point, new Vector3(0,-2,0), Quaternion.identity);
{
if(GameController.instance.isPoint == false && GameController.instance.EndMatch == false )
{
GameController.instance.number_PointsLeft++;
GameController.instance.isPoint = true;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public static GameController instance;
public Text txt_PointRight, txt_PointLeft;
public int number_PointsRight, number_PointsLeft;
public bool isPoint, EndMatch;
private void Awake()
{
if(instance == null)
{
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
txt_PointLeft.text = number_PointsLeft.ToString();
txt_PointRight.text = number_PointsRight.ToString();
}
}
the the first script is attached to the ball and the second script is the gamecontroller.
its a volleyball game where when a ball collides with the ground the score should increase for the winning player, but in my case the score increases from 0 to 1 then stops counting. any help??
Comment
GameController.instance.number_PointsLeft += 1;
see if it makes any difference.
Answer by DenisIsDenis · Jun 08, 2021 at 10:06 AM
You have a condition:
if(GameController.instance.isPoint == false && GameController.instance.EndMatch == false )
where the variable GameController.instance.isPoint
never becomes false
after the first goal is scored.
Therefore, next goals scored are not conditionally defended. You need to equate GameController.instance.isPoint
to false
when inning the ball:
GameController.instance.isPoint = false;