Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Newbie! Need Help with trigger and scoring!

Discussion in '2D' started by Warau, Aug 5, 2014.

  1. Warau

    Warau

    Joined:
    Jul 28, 2014
    Posts:
    12
    I'm new to Unity and scripting/programming, but can at least understand basic concepts and scripts.
    So I'm trying to make a 2D game similar to basketball. I need help with scripting a ball going into a box(trigger) that registers a +1 score and resets/respawns the ball to it's position from game start(not sure if I have to script ball spawn point too.).
    Will accept both C# and UnityScript. Thanks! :D
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    make ball with rigidbody and collider, set ball`s tag "ball". Create prefab for ball. Make box with collider, set box colider to trigger. Make empty gameobject for spawn.

    script for box will be some thing like:

    Code (CSharp):
    1.  
    2. public int score;
    3. public Transform ballspawn;
    4. public GameObject ball;
    5.  
    6. void OnTriggerEnter2D (Collider2D other)
    7. {
    8. if (other.gameObject.tag == "ball")
    9. {
    10. score +=1;
    11. Destroy (other.gameObject);
    12. Instantiate (ball, ballspawn.transform.position, ballspawn.transform.rotation);
    13. }
    14. }
    i didnot check it, may be it is somewhere wrong.
     
    Last edited: Aug 5, 2014
  3. Warau

    Warau

    Joined:
    Jul 28, 2014
    Posts:
    12
    Not sure if I did it properly but I got an error,

    "A namespace can only contain types and namespace declarations"


    Edit:

    I tried checking if everything is fine. Unity told me that Destroy and Instantiate didn't exist in current context. So I tried removing both to check if the box recognized the ball as something that would activate the trigger. It does. I'll try fiddling a bit more with the code. But if you can help me some more, I'd totally appreciate it :)
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class insideBorder : MonoBehaviour{
    5. public int score;
    6. public Transform ballspawn;
    7. public GameObject ball;
    8.  
    9. void OnTriggerEnter2D (Collider2D other)
    10. {
    11. if (other.gameObject.tag == "ball")
    12. {
    13.  
    14. score +=1;
    15. destroy(other.gameObject);
    16. instantiate (ball, ballspawn.transform.position, ballspawn.transform.rotation);
    17. Debug.Log("Positive");
    18. }
    19. }
    20. }
    21.  
     
    Last edited: Aug 5, 2014
  4. Tutelage Systems

    Tutelage Systems

    Joined:
    Jun 7, 2013
    Posts:
    6


    The code that vakabaka gave you isn't the full class. Put that code inside your class declaration. Just leave everything that Unity3d creates and paste the code inside the class like

    class YourClassName extends .... {
    // place in here
    }​
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    i am newbie too :)

    try Destroy Instantiate. I have just make it wrong. I have edited my script.

    ps. i think, if you have scripted something and it works with 1st run, then something is bad ;)
     
    Last edited: Aug 5, 2014
  6. Warau

    Warau

    Joined:
    Jul 28, 2014
    Posts:
    12
    Wew it kind of works now it destroys the ball and gives score(?). But still trying to figure out how to make it respawn.

    Still super thanks vakabaka :)
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    1. Instantiate (ball, ballspawn.transform.position, ballspawn.transform.rotation); this line should work for respawn.
    create prefab, drag&drop your ball to the prefab. Create empty for spawnpoint. Drag&drop ballprefab and emptypoint to the script in unity inspector. If you start your game in unity, destroy the ball and check hierarchy. The game schould create ball(clone), if one is destroyed. May be you can not see the ball, if it is behind the camera on z-direction (in that case move empty-point).
     
    Last edited: Aug 6, 2014