How to disable objects by tag name "Block" using button

Hello again. Does anyone know how to hide objects by its tag using button. Please check the script below. All i have done

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

public class BlockViewer : MonoBehaviour {

private GameObject cubeObject;

void Start()

{
    cubeObject2 = GameObject.FindGameObjectWithTag("Cube");

}
public void Disable()
{
    cubeObject.SetActive(false);
}

public void Enable()
{
    cubeObject.SetActive(true);
}

}

You are setting the object as cubeObject2 in Start() and then in Disable and Enable setting active cubeObject without the number. It should be the same (also, you declare the name as cube object before Start() so don’t forget to change that one if needed).

what u mean?

Like this?

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

public class BlockViewer : MonoBehaviour {

private GameObject cubeObject;

void Start()

{
    cubeObject = GameObject.FindGameObjectWithTag("Cube");

}
public void Disable()
{
    cubeObject.SetActive(false);
}

public void Enable()
{
    cubeObject.SetActive(true);
}

}

i am getting this error

UnassignedReferenceException: The variable cubeObject of BlockViewer has not been assigned.
You probably need to assign the cubeObject variable of the BlockViewer script in the inspector.
BlockViewer.Enable () (at Assets/Scene/Scripts/BlockViewer.cs:22)

I checked it and this seems to be working for me… Are you sure you added the Cube tag to the cube?
In the inspector:

Are u attacking the the script to emtyobject right?

keep having this error

NullReferenceException: Object reference not set to an instance of an object
TagDisabler.Enable () (at Assets/Scene/Scripts/TagDisabler.cs:23)

I wasn’t attaching it to the empty game object, but I did now, and it works all the same.
Your error seems to be changed, also the script name is different? Maybe I will try showing you how it looks in my code, as I need to leave the house now. Or maybe someone else could help you better.

Empty game object with script:

The cube:

The code (I’m calling disabled function in start to check if it works):

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

public class CubeEnable : MonoBehaviour {

private GameObject cubeObject;

void Start()

{
    cubeObject = GameObject.FindGameObjectWithTag("Cube");
	Disable();
}


public void Disable()
{
    cubeObject.SetActive(false);
}

public void Enable()
{
    cubeObject.SetActive(true);
}
}

I did everything exactly you told me to do but still getting same error.

The class you wrote is called BlockViewer, but the exception says you’re using the TagDisabler class. So you’re actually calling an Enable() method from another class.

If you’re doing it via a button, you need to assign to it the right game object (the one with the BlockViewer script), and then choose the BlockViewer.Disable (or Enable) method.

Hello Galandil i have fixed the class and everything but still keep getting the same error can you check for me please


And i am using this script now

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

public class CubeEnable : MonoBehaviour {

private GameObject cubeObject;

void Start()

{
cubeObject = GameObject.FindGameObjectWithTag(“Cube”);
Disable();
}

public void Disable()
{
cubeObject.SetActive(false);
}

public void Enable()
{
cubeObject.SetActive(true);
}
}

LoL now error is gone but can’t hide objects named Block in the scene using button. I am confused :frowning:

Seems like you have more than one game object with the Cube tag.

Add this line in Start to know how many objects are tagged as Cube:

If there’re more than one, keep in mind that the cube var will reference only the last object with the tag in hierarchy (the one nearest the bottom).

I got 44 objects tagged Cube

Thought so, it was the only possible explanation.

If your goal is to disable/enable ALL objects with a specific tag, you just have to write the class this way:

[code]using System.Collections;
using UnityEngine;

public class CubeEnable : MonoBehaviour {

private GameObject[] cubeList;

    void Start(){
        cubeList = GameObject.FindGameObjectsWithTag("Cube");
    }

    public void Disable(){
        foreach(GameObject obj in cubeList){
            obj.SetActive(false);
        }
    }

    public void Enable(){
        foreach(GameObject obj in cubeList){
            obj.SetActive(true);
        }
    }

}[/code]

However, I’d strongly advice that you follow some course about OOP, you’re lacking so far (based on your last 2-3 help requests on the forum) basic understanding of how to code and how OOP works. Take a break from Unity and do that, it’ll prove very useful. :wink:

Rather than giving Tag to object can i try like this one

//this will be attach to empty game object

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

public class PinDisabler : MonoBehaviour
{
public Transform container = null;

public void Disable()
{

    container.gameObject.SetActive(false);

}

public void Enable()
{

    container.gameObject.SetActive(true);

}

}

// This will be attach to Pins that i disable them

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

public class Pin : MonoBehaviour

{
PinDisabler container;

void Start()
{
    container = GetComponentInParent<PinDisabler>();
}

}

If the game objects to disable are all children of the container game object parent, you just need this on the container game object:

[code]using System.Collections;
using UnityEngine;

public class CubeEnable : MonoBehaviour {

public void Disable(){
        foreach (Transform child in transform){
            child.gameObject.SetActive(false);
        }
    }

    public void Enable(){
        foreach (Transform child in transform){
            child.gameObject.SetActive(true);
        }
    }

}[/code]

Privacy & Terms