Search Unity

Collision help

Discussion in 'Scripting' started by batista12, Oct 7, 2015.

  1. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    I need help with function OnCollisionEnter. When i hit one target with the bullet this script don't do anything. This script is attatch on Player, if I attatch on bullet object, variable points reset every time I shot other bullet. Please help me guys.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class points : MonoBehaviour
    5. {
    6.     public int points;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         points = 0;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         MaxPontos ();
    16.  
    17.     }
    18.     void OnCollisionEnter(Collision target) {
    19.        
    20.         if(target.gameObject.tag == "Enemy") {
    21.             points = points + 1;
    22.             print("" + points);
    23.         }
    24.     }
    25.     void MaxPontos(){
    26.         if (points >= 15)
    27.         {
    28.             Application.LoadLevel("scorelevel");
    29.         }
    30.     }
    31.     void OnGUI(){
    32.         GUI.Box(new Rect(600,10,90,25 ), "Score : " + points);
    33.     }
    34. }
    35.  
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Does either the bullet object or the enemy have a rigidbody attached to it?
     
  3. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    Yes, booth have rigidbody attached.
     
  4. Cyril5

    Cyril5

    Joined:
    May 6, 2013
    Posts:
    6
    Did you try the collision with the player and an object without rigidbody like a wall ?
    I think OnCollisionEnter not work with two dynamics object, use OnTriggerEnter instead.
     
  5. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    When pass on target with player, increase 1 in variable points, but when hit with bullet nothing happens
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What you may want to try is having the collision check on your bullet script & the points stored on the player or a game manager script/object. When the bullet hits the enemy access the points variable stored on the other script & update it.
    If the script above is in the bullet then you are updating the points value stored on each bullet, if it is in the player then it only updates when the player collides with the enemy.

    & OnTriggerEnter is probably better
     
  7. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    But idk how I do this. I started code on unity a month ago and I just have learned some functions
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    OnCollision### and OnTrigger### are called when the colliders on the same gameobject (or children) as this script collide with something. So the if the above script is on the player, the player would need to collide with the enemy to score points.

    If you want to shoot the enemy with a bullet to score points, you will need to put the collision code on the bullet.

    Tracking the overall score isn't done on bullets, they just need to tell another "GameManager", "ScoreManager", "whateverYouWantToCallItScript" that they have collided and the single instance of this other script keeps track of the one score.


    also, don't use the old legacy GUI if you are starting out, use the new UI https://unity3d.com/learn/tutorials/topics/user-interface-ui :D
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    https://unity3d.com/learn/tutorials/topics/scripting

    live sessions 1, 2, 3 and 8... one of them will cover off getting scripts and gameobjects to work together I would think
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    It's a good idea to work through the tutorials, doing them as you go, & then testing yourself by either modifying the project with new stuff you think of or applying it to your own simple project. That way it reinforces what you learnt & gets you to think about how you use it in your own situation.
     
  11. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    I have read some tutorials and questions on forum and i test this code bellow. I attach on Player and i recivied this error:
    Assets/Scripts/potnso.cs(17,49): error CS0120: An object reference is required to access non-static member `BULLET_ThermalDetonator.points'
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class potnso: MonoBehaviour {
    5.     public int score;
    6.     public int scorefinal;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         MaxPontos ();
    15.         GameObject bullet_prefab = GameObject.Find("bullet_prefab");
    16.         BULLET_ThermalDetonator _BULLET_ThermalDetonator = bullet_prefab.GetComponent<BULLET_ThermalDetonator>();
    17.         score = BULLET_ThermalDetonator.points;
    18.         if (score == 1) {
    19.             scorefinal=scorefinal+1;
    20.         }
    21.  
    22.     }
    23.     void MaxPontos(){
    24.         if (scorefinal >= 15) {
    25.             Application.LoadLevel("scorelevel");
    26.         }
    27.     }
    28. }
    29.  
     
  12. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    is the points on the bullet prefab public?
    I would've made the bullet find the points script & increment the score that way. Currently every frame your potnso script is looking for a bullet prefab but if you have more than one bullet in the scene at once then I don't think it will find them all
     
  13. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    Yes, points is public. On bullet script, every bullet destroy in 3 seconds. If i put this time in 1 second, maybe it doesn't have more than 1 bullet
     
  14. batista12

    batista12

    Joined:
    Oct 7, 2015
    Posts:
    7
    Apparently, the script can't found bullet's gameObject. I try a lot of thing but nothing works.
     
  15. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    step through from the beginning. Why should the manager/score script keep track of every bullet? Why can't each bullet find the manager/points script & then update the points when needed (not every bullet will hit the enemy)?

    From that I would have each bullet find the script/object that has the score stored & then update that value as needed.