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

Add prefab on MouseDown Destroy on MouseUp -> repeat

Discussion in 'Scripting' started by schmidicow, Mar 23, 2015.

  1. schmidicow

    schmidicow

    Joined:
    Mar 23, 2014
    Posts:
    10
    Hi everyone,

    I'm new to Unity (and more or less to programming).
    I've made an animated Cube, this should appear when the left mouse button is clicked.
    And removed when the left mouse button is released.

    But on the second click i get this error:
    I'm really lost at this point, so i hope someone can help me with this.

    This is my Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainGame : MonoBehaviour {
    5.  
    6.     public GameObject showTimer;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetMouseButtonDown (0)) {
    16.  
    17.             showTimer = (GameObject) Instantiate(showTimer, new Vector3(0,0,0),Quaternion.identity);
    18.  
    19.         }
    20.  
    21.         if (Input.GetMouseButtonUp (0)) {
    22.  
    23.             Destroy(showTimer);
    24.  
    25.         }
    26.     }
    27. }
    28.  
    Thanks for helping
    -s
     
  2. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    As the error says, you are trying to access a destroyed object.
    Code (csharp):
    1.  
    2. [*]using UnityEngine;
    3. [*]using System.Collections;
    4. [*]
    5.  
    6. [*]public class MainGame : MonoBehaviour {
    7. [*]
    8.  
    9. [*]   public GameObject showTimer;
    10. [*]   private GameObject showTimerClone;
    11. [*]
    12.  
    13. [*]   // Use this for initialization
    14. [*]   void Start () {
    15. [*]
    16.  
    17. [*]   }
    18. [*]  
    19. [*]   // Update is called once per frame
    20. [*]   void Update () {
    21. [*]       if (Input.GetMouseButtonDown (0)) {
    22. [*]
    23.  
    24. [*]            showTimerClone = (GameObject) Instantiate(showTimer, new Vector3(0,0,0),Quaternion.identity);
    25. [*]
    26.  
    27. [*]       }
    28. [*]
    29.  
    30. [*]       if (Input.GetMouseButtonUp (0)) {
    31. [*]
    32.  
    33. [*]           Destroy(showTimerClone);
    34. [*]
    35.  
    36. [*]       }
    37. [*]   }
    38. [*]}
    39.  
    This will do the trick, but instantiating and destroying an object like this is a bad habit.
     
  3. schmidicow

    schmidicow

    Joined:
    Mar 23, 2014
    Posts:
    10
    Thank you,
    so i shouldn't destroy my instance, but a clone of my instance, sounds logic ... now.

    Thank you, works great now.

    I will work on the Code again, found the setActive function.