Search Unity

Make object disappear

Discussion in '2D' started by gflint, Jan 10, 2019.

  1. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    I am a novice at Unity so a lot of trial and error is involved here. I want an object to disappear when I click on it. Below is what I have now. When I click on the Post object both the Post and the BlueCube disappear simultaneously. Hummm. When I click on the BlueCube only the BlueCube disappears. I have this script attached to both the Post object and the BlueBox object in the Inspector. This also seems like a very inefficient way to accomplish the task. If I get it working there has to be an If statement for every object. There has got to be a better way.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ClickDisappear : MonoBehaviour
    7. {
    8.     void Start()
    9.     {      
    10.     }
    11.     void Update()
    12.     {
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    16.             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    17.             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    18.  
    19.             if (hit.collider != null && hit.collider.gameObject.name == "Post")
    20.             {
    21.                 gameObject.active = false; //hide
    22.             }
    23.             if (hit.collider != null && hit.collider.gameObject.name == "BlueCube")
    24.             {
    25.                 gameObject.active = false; //hide
    26.             }
    27.         }
    28.     }
    29. }
     
  2. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    Well, one solution you could try is to have a script attached to each object, and have an OnCollision function.
    So the objects themselves can hide themselves on hit. I've made that for my BlockBreaker game when I was learning about physics in Unity, it looked like this;

    public class BlockController : MonoBehaviour
    {
    private bool destroyBlock;

    // Update is called once per frame
    void Update()
    {
    if (destroyBlock)
    {
    destroyBlock = false;
    AudioSource audio;
    audio = GetComponent<AudioSource>();
    audio.Play();
    GetComponent<MeshRenderer>().enabled = false;
    GetComponent<Collider>().enabled = false;
    Destroy(gameObject, 2);
    }
    }
    void OnCollisionEnter(Collision col)
    {
    destroyBlock = true;
    }
    }


    Each, prefab, block, has this script attached. If the destroyBlock boolean is set, as is done in OnCollisionEnter. You could also make the destroyBlock boolean public and then grab the object the RayCast is hitting, typecast that and destroy the object that way.

    The key here, I'd say, is generalize and have the objects 'control' themselves based on input and variables. Does that make sense for you?
     
  3. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    Found the problem. This seems to work. The hit.collider.gameObject.SetActive(false) was the issue. I apparently was not telling it which collider to look at. This also gets rid of all the If business. The gameObject.SetActive(false) seems to be the same as gameObject.active = false but is the newer syntax. I am still getting "NullReferenceException: Object reference not set to an instance of an object" error but that does not seem to effect the workings. Is this something to worry about?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ClickDisappear : MonoBehaviour
    {
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    hit.collider.gameObject.SetActive(false); //hide
    }
    }
    }
     
  4. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    This does make sense. It will take some fiddling to get it to work in my case. I will tinker this weekend. Thanks.
     
  5. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    Now my problem is that object that I do not want to disappear when clicked on disappear. The script seems to be working globally. How do I restrict it to only the correct objects?
     
  6. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    You attach the script to each individual object. Making in trigger only on their own events.
     
  7. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    I have the script attached to the blue objects only (if by attached you mean listed in the Inspector for that object. I dragged the script to the Inspector to get it there.) The red objects do not have the script. How do I make it trigger only on there events? I tried selecting "Is Trigger" for the blue objects but that just resulted in the blue objects falling through the floor they are sitting on.
     
  8. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    An interesting observation I just noticed. If I make all the blue objects disappear by clicking on them (I want the blues to disappear) then the red objects will not disappear when clicked on. So if all the objects with the script attached are gone the script no longer affects the other objects that do not have the script attached..
     
  9. gflint

    gflint

    Joined:
    Dec 20, 2018
    Posts:
    13
    Got it. I was missing the If statement to check if the hit.collider clicked on was the object the script was attached to.

    using System.Collections.Generic;
    using UnityEngine;

    public class ClickDisappear : MonoBehaviour
    {

    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    if (hit.collider.gameObject == gameObject)
    {
    gameObject.SetActive(false); //hide }
    }
    }
    }
    }