Search Unity

Re: Code error or object error or what?

Discussion in '2D' started by motionlife89, Jul 20, 2014.

  1. motionlife89

    motionlife89

    Joined:
    Dec 23, 2013
    Posts:
    19
    Good day Unity members!

    I am making a game in which my player (runner) must avoid getting hit by some obstacles and must destroy
    an enemy ( food) which is a prefab. Upon collision with this enemy ( food), a sound is played and the player earns a score.

    However, there are a few problems when I tried previewing my game.

    1) When I tried to play the game, the player does not die when it collides with an obstacle.

    2) When player (runner) collides with food, a sound is played but it KILLS player (runner) instead.

    3) Why is the music already playing when I tried to play the game? it should only play when player (runner) collides with food.

    Question: what went wrong? I have already asked some members here and followed their suggestions. Unfortunately, the code isn't working as expected once I play the game.

    I am attaching the codes and some screenshots for reference. Please let me know if I have missed something or some better solutions to my dilemma. Thank you.


    Code for runner ( player)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class runner : MonoBehaviour {
    6.    
    7.     public Vector2 jumpForce = new Vector2(0, 1);
    8.  
    9.     public AudioClip[] blips;
    10.    
    11.     void Update () {
    12.        
    13.         if (Input.GetKeyUp("space")) {        
    14.             rigidbody2D.velocity = Vector2.zero;          
    15.             rigidbody2D.AddForce(jumpForce);          
    16.         }
    17.        
    18.         Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
    19.        
    20.         if (screenPosition.y > Screen.height || screenPosition.y < 0)    
    21.             Die();
    22.     }
    23.    
    24.  
    25.     void OnCollisionEnter2D (Collision2D other) {
    26.        
    27.         if (other.gameObject.tag == "food")
    28.         {
    29.             Debug.Log("Collided food !");
    30.  
    31.             audio.PlayOneShot(blips[0]); //part that changed
    32.             Destroy (other.gameObject);
    33.         }
    34.  
    35.    
    36.         else
    37.             Die();
    38.     }
    39.  
    40.    
    41.     void Die () {
    42.         Destroy(this.gameObject);
    43.         Application.LoadLevel(Application.loadedLevel);
    44.     }
    45. }
    46.  
    Code for food

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class foodpowgen1 : MonoBehaviour {
    5.  
    6.  
    7.     public GameObject sans;
    8.     int score = 0;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.    
    14.     void Start()
    15.     {
    16.         InvokeRepeating("CreateObstacle", 1f, 1.5f);
    17.     }
    18.    
    19.  
    20.     // Update is called once per frame
    21.     void OnGUI ()
    22.     {
    23.         GUI.color = Color.black;
    24.         GUILayout.Label(" Score: " + score.ToString());
    25.     }
    26.  
    27.    
    28.     void CreateObstacle()
    29.     {
    30.         Instantiate(sans);
    31.         score++;

    Code for obstacle:



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class obstacle : MonoBehaviour {
    5.  
    6.     public Vector2 velocity = new Vector2(-4, 0);
    7.    
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         rigidbody2D.velocity = velocity;
    12.     }
    13. }
    These are the respective screenshots of my player, obstacle and food.

    Player's screenshot:



    obstacle screenshot:







    food screenshot:



     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    1) Maybe put a Debug.Log at the very beginning of your OnCollisionEnter2D so you can find out whether the function isn't processing the tag correctly, or just isn't getting called.

    2) You didn't actually post the food code? It's the obstacle generator?

    3) You've got "Play on Awake" checked in the audio source.
     
  3. motionlife89

    motionlife89

    Joined:
    Dec 23, 2013
    Posts:
    19
    Thanks for your response. I did uncheck the "Play on Awake". It works but problem # 2 persists.

    I added Debug.Log at the runner's code. when I played the game, I saw that it did collided with the player and the music played but I don't understand why my player was killed by it. it should be the runner destroying the food.

    The obstacle generator is different from 'food.' The runner should not hit the obstacle otherwise it dies.
    As the for the code of the food which was posted, it's the script for the prefab food. If you want the code for the 'food', here it is:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class foodpow : MonoBehaviour {
    5.  
    6.     public Vector2 velocity = new Vector2(-4, 0);
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         rigidbody2D.velocity = velocity;
    12.     }
    13. }


    I would like to know what's the mistake I made and why my game isn't playing as I expected it to be.