So I am trying to make a game like fruit ninja, in which you basically click on a prefab using mouse and it destroys it, updating your score by 5. If you click on the bad prefab, you loose 5 points. If a good prefab falls on the sensor, an OnTriggerEnter method destroys the object and should run the gameOver Function, which should show a text Game Over. But the If statement that that triggers this is not working.
The target Mover script:
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class targetMover : MonoBehaviour
{
private gameManager gameManagerScript;
private Rigidbody targetRb;
int minForce = 16;
int maxForce = 18;
int minTorque = -10;
int maxTorque = 10;
int minPosX = -4;
int maxPosX = 4;
int posY = -6;
public int pointValue;
public ParticleSystem explosionParticle;
public bool hasCollided = false;
// Start is called before the first frame update
void Start()
{
targetRb = GetComponent<Rigidbody>();
//Adding Force, torque and assigning position to GameObject
targetRb.AddForce(randomForce(), ForceMode.Impulse);
targetRb.AddTorque(randomTorque(), randomTorque(), randomTorque(), ForceMode.Impulse);
transform.position = randomPos();
gameManagerScript = GameObject.Find("gameManager").GetComponent<gameManager>();
}
// Update is called once per frame
void Update()
{
}
private void OnMouseDown()
{
gameManagerScript.updateScore(pointValue);
Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
Destroy(gameObject);
}
private Vector3 randomForce() //Generates a Random force and returns the random Vector
{
return Vector3.up*Random.Range(minForce, maxForce);
}
private float randomTorque() //Generates a Random Torque and returns the random float
{
return Random.Range(minTorque, maxTorque);
}
private Vector3 randomPos() //Generates a Random Force and returns the random Vector
{
return new Vector3(Random.Range(minPosX, maxPosX), posY);
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
if (other.gameObject.CompareTag("Target"))
{
gameManagerScript.gameOverMethod();
Debug.Log("Object Fell");
}
}
}
The game Management script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class gameManager : MonoBehaviour
{
public TextMeshProUGUI scoreText;
public List<GameObject> targets;
public int playerScore;
public TextMeshProUGUI gameOver;
// Start is called before the first frame update
void Start()
{
playerScore = 0;
StartCoroutine(spawnTargets());
updateScore(0);
}
// Update is called once per frame
void Update()
{
}
IEnumerator spawnTargets()
{
while (true)
{
yield return new WaitForSeconds(1);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}
public void updateScore(int scoreToAdd)
{
playerScore += scoreToAdd;
scoreText.text = "Score:" + playerScore;
}
public void gameOverMethod()
{
gameOver.gameObject.SetActive(true);
}
}
Please help with this problem. I am new to unity