- Home /
Balls Collision and Destruction = Higher Score. HELP ME?
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);
}
}
}
Answer by UnityDevelopper · May 28, 2014 at 03:30 AM
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);
}
}
it is attached to the finishline and when the balls enter the finishline box, i want to a point to the ball count and destroy the ball.
do Destroy(collison.gameObject), and don,t do Destroy(other.gameObject) because you name your on trigger parameter to collision, if you want to use other change your code to this
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Ball")
{
other.gameObject.SetActive(false);
count += 1;
SetCountText();
Debug.Log ("Score" + count);
Destroy(other.gameObject);
}
}
After a break and returning to the game, I finally got it. I needed to add void OnTriggerEnter2D (Collider2D other) and some buttons on console panel to see it actually work. smh :P THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!!
Answer by An-Item · May 28, 2014 at 03:29 AM
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);
}
}
Thank you so much for responding. I want the balls to be destroyed when they collide with the finishline. I dont want the finishline to be destroy. If I change the code to Destroy (other.gameobject). would that help? :S
Answer by siddharth3322 · May 28, 2014 at 03:01 AM
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.
So i want the balls to be destroyed when it gets in contact with the finishline. Looking at your reply, i applied what you said and tried putting the script on the balls and that didnt work. :S