Search Unity

Basic Destroy Gameobject Script Not Working

Discussion in 'Scripting' started by Littlenorwegian, Feb 12, 2016.

  1. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    OK, this is odd. I can't get this to work.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.     public GameObject Player;
    6.     void Start () {
    7.         if (Input.GetButtonDown("Fire1"))
    8.         {
    9.             Destroy (Player);
    10.         }
    11.     }
    I attach the script to a gameobject in the scene and set the Player. Obviously, doing something wrong.
    I've googled and tried a different handful after my own attempts and obviously I am overlooking something.

    Thank you for your help.
     
  2. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    put your code in update function
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    start is called once at the beginning of the life of a gameobject, the chances of pressing the right input at exactly that fraction of a second is rather low.

    update is called every frame... so yeah, update not start :)
     
  4. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Haha, think that's my biggest derp in a very long time.
    Thank you for being so understanding. :)