Make object act as a hinder and dissappear by pressing a button

So I want my wall to act like a hinder and won’t go away unless you press a certain button. I found this script that is said to make your object go away. I have error on my code so I’m wondering how to fix it?

// Use this for initialization
    void Start () {
        GetComponent<Renderer>.enabled = false;
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

Hi,

try this:

using UnityEngine;

public class YourClassName
{
    Renderer myRenderer;

   void Start()
   {
    myRenderer = GetComponent<Renderer>(); 
   }

   void Update()
   {
        if(Input.GetButtonDown("e.g. Fire1"))
        {
             myRenderer.enabled = false;
        }
    }
}

You have missing the using directives in your piece of code. Also you have link the Renderer-Component to an Input-Event likewise in my script.

1 Like

Thank you very much it worked! I was thinking about adding preassure so you can’t pass the object without right click away the object does it work with adding collision or do I need to code it?

I didn’t understand exactly what you mean, but when you want automatically disable your Object by hitting colliders, so you have to set an Event with OnCollisionEnter() or similar Collision functions.
Of course you have to script this. Such situations are very well discribed in the Manual and Scripting API.

I mean that the object I now thanks to you can dissapear when I press a button I want it so that my ball can’t pass it unless I press the button. Because now it can simply pass it and I don’t want that.

The renderer is just a visual thing. It doesn’t prevent objects going through it.

Have you gone through some tutorial material: Learn ?

I have gone through the 2d UFO tutorial and then there was another one I watched I don’t remember the name but talking about colliders so that’s what I’m wondering if I should use it in my case? Though I feel like when I press Fire1 and the wall is gone and I have added say collision won’t there be an invisable wall then? I’m then thinking about instead just adding adding 2d box collider to the wall since it has a trigger option you can apply but I’m not sure or you can make a script for it?

This is why going over some more of the tutorial lessons will be a great help to you with Unity & building your game.
You would learn about colliders and triggers, for instance.

You can setup colliders (non-trigger) to prevent your ball from going through, if it’s moving with physics. That is, it’s handled for you that way. Instead of simply disabling the renderer, you could disable the game object.

Thank you I looked more into the collider function and it worked on the object to stop the ball though when I press fire1 for it to go away I still can’t pass it?

Did you deactivate the game object? Does it appear deactivated in the hierarchy?

I don’t think so unless it says it is deactivated?

What I mean is, if you were blocked by the collider, and pressed the button to make it go away, hopefully you disabled the game object (so it can’t be seen, or stop you from passing).
If you can try doing that & write back about your success …? :slight_smile:

Feel free to post your code, if you are stuck, too.

I tried mark “is trigger” but then the object is not blocking at all

I’m using this code

public class Hinder : MonoBehaviour
{
    Renderer myRenderer;

    void Start()
    {
        myRenderer = GetComponent<Renderer>();
    }

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            myRenderer.enabled = false;
        }
    }
}

Yep, trigger won’t work, nor will simply disabling the renderer.

Try changing your script to this:

public class Hinder : MonoBehaviour {

void Update() {
   if(Input.GetButtonDown("Fire1")) {
       gameObject.SetActive(false);
     }
 }

That’s all… :slight_smile:

Thank you very much!