Search Unity

How do i make a object Disapear and reaper

Discussion in 'Getting Started' started by ChaytonTime, Feb 14, 2019.

  1. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    i really need help here is the code i am working with i saw it on youtube i did it and it did not work so yeah and i am a beginner so please help

    void Start()
    {
    gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyUp(KeyCode.Z))
    {
    gameObject.SetActive(true);
    }
    }
    }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    Once a GameObject is disabled every script on it will cease functioning. There are a couple ways to handle this. One is to disable the renderer and disable every other component that would allow the object to interact with other objects.

    Code (csharp):
    1. void Start()
    2. {
    3.     // hide the object
    4.     GetComponent<Renderer>().enabled = false;
    5.  
    6.     // turn off any colliders
    7.     foreach (Collider c in GetComponents<Collider>())
    8.         c.enabled = false;
    9. }
    10.  
    11. void Update()
    12. {
    13.     if (Input.GetKeyUp(KeyCode.Z))
    14.     {
    15.         // show the object
    16.         GetComponent<Renderer>().enabled = true;
    17.  
    18.         // turn on any colliders
    19.         foreach (Collider c in GetComponents<Collider>())
    20.             c.enabled = true;
    21.     }
    22. }
    Your second option is to just move the object way off screen, but then if the object is supposed to do anything besides be "hidden" you will need to check to see if it's been moved off-screen. As a side note the way I'm using the boolean here to allow actions to be performed is part of what you would use to create a pause system.

    Code (csharp):
    1. private Vector3 originPos;
    2. private bool isVisible = false;
    3.  
    4. void Start()
    5. {
    6.     // remember the origin position
    7.     originPos = transform.position;
    8. }
    9.  
    10. void Update()
    11. {
    12.     if (Input.GetKeyUp(KeyCode.Z))
    13.     {
    14.         // move object back
    15.         transform.position = originPos;
    16.  
    17.         // change the boolean to enable any other operations
    18.         isVisible = true;
    19.     }
    20.  
    21.     if (isVisible)
    22.     {
    23.         // actions that will be performed if the object is visible
    24.     }
    25. }
     
  3. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    Hello Ryiah so i am a beginner and idk what this code is i did the first opition that you said and it did not work soooo yeah can you please help do you have a way to explain it more clearly like you are teaching a beginner that has only had like 3-6 months of stuff known
     
    Last edited: Feb 14, 2019
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    To be honest there isn't much to explain. All of the code in my post should have been covered in the tutorials on this site if you've been following along with them (ie typing up the code they present), but just in case you've been using an alternative way to learn I'll try to explain them.

    What makes a game object visible on screen is a renderer (MeshRenderer renders a mesh, ParticleSystemRenderer renders particles, etc). What this line of code does is just turn it off. Once it's turned off the object will no longer be visible.

    Code (csharp):
    1. GetComponent<Renderer>().enabled = false;
    That said just because it's invisible doesn't mean it can't be interacted with and that's what the next section is for.

    Colliders have two basic purposes. One of those is to detect when two objects are trying to run into each other (ie they're colliding). What the code does below is scan for colliders and try to disable them. It's very likely the problem you're having is from this code section. Feel free to remove it if you don't have colliders.

    Code (csharp):
    1. foreach (Collider c in GetComponents<Collider>())
    2.     c.enabled = false;
    The second use of colliders is to just detect when one object has entered it. We still refer to them as colliders but they're much more like triggers than colliders. An example of a trigger zone would be a hallway of laser beams where you can pass through the beams but doing so triggers an alarm. Each beam would need a collider set to behave as a trigger.
     
  5. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    OK so but when i use this code it does not work plz help :(
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Knife : MonoBehaviour
    6. {
    7.    
    8.  
    9.     void Start()
    10.     {
    11.         GetComponent<Renderer>().enabled = true;
    12.     }
    13.    
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.         if (Input.GetKeyUp(KeyCode.Z))
    20.         {
    21.             // show the object
    22.             GetComponent<Renderer>().enabled = false;
    23.         }
    24.     }
    25. }
    26.  
     
  6. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    what i am trying to do is make a knife appear and dissapear its a child of a person when they press Z i want the knife to appear so he can attack
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    What's the error message in the console? Or does it simply not do anything?
     
  8. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    it does not do anything
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    Oh, whoops, I know what's wrong and it's one of those things that gets me from time to time too (as this thread does a great job of proving). GetKeyUp checks if a key is not being pressed. GetKeyDown is the one to check if the key has been pressed. Line 16 is the only line I changed.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Knife : MonoBehaviour {
    6.  
    7.  
    8.     void Start() {
    9.         GetComponent<Renderer>().enabled = true;
    10.     }
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update() {
    15.  
    16.         if (Input.GetKeyDown(KeyCode.Z)) {
    17.             // show the object
    18.             GetComponent<Renderer>().enabled = false;
    19.         }
    20.     }
    21. }
     
  10. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    but when i do it and when i press the key it does not work it is a child does that do anything Untitled.png
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    That shouldn't matter. Only thing I can think of at this point is if the script isn't on the game object that has the knife mesh renderer and related. I'm out of ideas at this point.
     
    ChaytonTime likes this.
  12. ChaytonTime

    ChaytonTime

    Joined:
    Feb 12, 2019
    Posts:
    11
    how do you make a mesh
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    You can create them a few different ways. The normal way is to create them in an external program (eg Blender, Maya, etc), but you can create them in the editor too. Unity's basic shapes (cubes, spheres, etc) are one way. ProBuilder can be used to create them as well as creating entire level layouts.

    https://unity3d.com/unity/features/worldbuilding/probuilder
     
    Last edited: Feb 15, 2019