Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nzingha · May 28, 2014 at 01:58 AM · ontriggerenteroncollisionenter

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);
         }
         
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
        }
  
     }

 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityDevelopper · May 28, 2014 at 02:29 AM 0
Share

which object is this attached too

avatar image Nzingha · May 29, 2014 at 02:19 PM 0
Share

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.

avatar image UnityDevelopper · Jun 02, 2014 at 06:48 PM 0
Share

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);
            }
      
         }
avatar image Nzingha · Jul 27, 2014 at 06:09 PM 0
Share

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!!!

avatar image
0

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);
        }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nzingha · May 29, 2014 at 02:16 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nzingha · May 29, 2014 at 02:20 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cone of effect weapon FPS 0 Answers

Have bullets be destroyed when they collide with ANY collider 0 Answers

Transform collider not detecting collision on rigidbody collider 2 Answers

OnCollisionEnter() not getting called between two rigid bodies 3 Answers

Colliding two GameObjects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges