HHHHEEEELLLLPPPPP MEEEEEE, please?
I am designing a small game for school. The player gets the ball to the finishline and then it is destroyed. The ball count goes up one point. The balls have rigidbodies and the finishline is transparent plus it has a box collider. I even have GUI Text in there. One for showing the player how many balls they got to the finishline. Others for timer and name but that is not the focus. I don’t know why the finishline doesnot work. Please help me. FYI I am a UNITY novice.
using UnityEngine;
using System.Collections;
public class Finishline : MonoBehaviour
{
//declare scoring variables
public int count;
public GUIText countText;
public GUIText winText;
void Start ()
{
count = 0;
countText = "";
winText.text = "";
}
void SetCountText(){
countText.text = "Count: "+ count.ToString();
if(count>11)
{
winText.text = "You Win!";
}
}
//if the balls collides with a finishline collider, destroy the balls and add a point to score.
void OnTriggerEnter(Collider collison)
{
if(other.gameObject.tag == "Ball")
{
other.gameObject.SetActive(false);
count += 1;
SetCountText();
Debug.Log ("Score" + count);
Destroy(gameObject);
}
}
}
i not sure if this will help but change the other to collision
void OnTriggerEnter(Collider collison)
{
if(collision.gameObject.tag == "Ball")
{
collision.gameObject.SetActive(false);
count += 1;
SetCountText();
Debug.Log ("Score" + count);
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider collison) //your parameter name collision
{
if(other.gameObject.tag == “Ball”) //but you call other instead
{
other.gameObject.SetActive(false);
count += 1;
SetCountText();
Debug.Log ("Score" + count);
Destroy(gameObject); // do you need to destroy the finish line ?
}
}
try this:
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Ball")
{
other.gameObject.SetActive(false);
count += 1;
SetCountText();
Debug.Log ("Score" + count);
}
}
Here you have some misconcept in mind.
First I assume that you have assign this script to finish line , based on that my reply work for you.
You assign FinishLine script to FinishLine object so in gameobject you retrieve game object of finish line and you are destroying it rather than ball object.
Destroy(gameObject);
Instead of this you have to destroy ball object. That you do in following way.
Destroy(collision.gameObject);
I think there is no meaning of deactivating game object here. That you have done in this line.
other.gameObject.SetActive(false);
And in OnTriggerEnter() method you have used other as object but I can’t find any reference of it in current context. So you have to look for it also.