Activate/Deactivate Game Objects through buttons

Hello,
I’m trying to handling some game objects which are useful in my game in order to manage the weapons that could be used by the player. This is the script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class WeaponHandlerScript : MonoBehaviour {
    
    	public GameObject weaponPanel;
    	public int selected = 0;
    	public Button[] butts;
    	public Transform[] comps;
    	public GameObject shoot;
    	public GameObject jump;
    
    	void Start () {
    		weaponPanel = GameObject.FindGameObjectWithTag ("WeaponPanel");
    		butts = weaponPanel.GetComponentsInChildren<Button> ();
    		comps = weaponPanel.GetComponentsInChildren<Transform> ();
    
    		shoot = GameObject.FindGameObjectWithTag ("WeaponShoot");
    		jump = GameObject.FindGameObjectWithTag ("WeaponJump");
    
    	}
    	
    	public void someFunction() {
    		shoot.SetActive (true);
    		jump.SetActive (false);
    	}
    
    }

The function “someFunction” is attached to a button, in order to enable the shooter component and disable the jump one. However, I receive the following error:
UnassignedReferenceException: The variable shoot of WeaponHandlerScript has not been assigned.
You probably need to assign the shoot variable of the WeaponHandlerScript script in the inspector.

Any help?

Shoot isnt being found with the tag. Make sure your shoot gameobject has the proper tag.

you this code instead of your code

public void someFunction()

{

weapon=!weapon

if(weapon)

         shoot.SetActive (true);

else

         jump.SetActive (false);

}

Working on Transform component and through SetActiveInHierarchy, I solved the problem. Here’s the script: at runtime, I generate my own HUD plus I add an event on all buttons, turning on and off the game object representing the weapons available to the player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;

public class WeaponHandlerScript : MonoBehaviour {

	public Transform weaponPanel;
	public Transform selectPanel;
	public Transform[] selectChildren;
	public GameObject[] weapons;
	public Button[] butts;

	public Button theButt;
	public Transform[] childrenButt;
	private float buttPos;
	private float buttPosTemp;
	private Vector2 panelSize;
	private Vector2 panelSizeTemp;

	public string theName;
	// public 
	// Use this for initialization
	void Start () {
		weaponPanel = GameObject.FindGameObjectWithTag ("WeaponPanel").transform;
		selectPanel = GameObject.FindGameObjectWithTag ("SelectPanel").transform;
		selectChildren = selectPanel.GetComponentsInChildren<Transform> ();
		weapons = GameObject.FindGameObjectsWithTag ("WeaponSpawn");
		butts = weaponPanel.GetComponentsInChildren<Button> ();
		theButt = butts [0];

		if (weapons.Length == 0)
			return;
		// childrenButt[1] = the image; childrenButt[2] = selector
		// childrenButt = theButt.GetComponentsInChildren<Transform> ();

		buttPos = theButt.GetComponent<RectTransform> ().anchoredPosition.x;

		panelSize = weaponPanel.GetComponent<RectTransform> ().sizeDelta;

		for (int i = 0; i < weapons.Length - 1; i++) {
			panelSizeTemp = new Vector2 (panelSize.x + 43f, panelSize.y);
			panelSize = panelSizeTemp;
			weaponPanel.GetComponent<RectTransform> ().sizeDelta = panelSize;

			Button goButton = Instantiate (theButt) as Button;
			goButton.name = "ButtonWeapon" + (i+1);
			goButton.transform.SetParent(weaponPanel, false);
			goButton.transform.localScale = new Vector3 (1, 1, 1);

			RectTransform rectButt = goButton.GetComponent<RectTransform> ();
			rectButt.anchorMax = new Vector2 (0, 0.5f);
			rectButt.anchorMin = new Vector2 (0, 0.5f);
			buttPosTemp = buttPos + 45f;
			buttPos = buttPosTemp;
			rectButt.anchoredPosition = new Vector2 (buttPosTemp, 0);

		}

		butts = weaponPanel.GetComponentsInChildren<Button> ();
		selectChildren [1].GetComponent<Image> ().sprite = weapons [0].GetComponent<GenericWeapon> ().img;
		theName = weapons [0].GetComponent<GenericWeapon> ().nameWeap;
		selectChildren [2].GetComponent<TextMeshProUGUI> ().text = GameManagerScript.GetInstance ().GetAmmo (theName).ToString();

		for (int i = 0; i < butts.Length; i++) {
			Transform[] childrenButt = butts *.GetComponentsInChildren<Transform> ();*
  •  	if (i == 0) {*
    

_ weapons .SetActive (true);_
* childrenButt [2].gameObject.SetActive (true);*
* }*
* else {*
_ weapons .SetActive (false);
* childrenButt [2].gameObject.SetActive (false);
}*_

_ childrenButt [1].GetComponent ().sprite = weapons .GetComponent ().img;
AddEvent (butts, butts , weapons .name, weapons, selectChildren[1], selectChildren[2]);
* }
}*_

* void Update () {*
* for (int i = 0; i < weapons.Length; i++) {*

* string weapAmmos;*

_ if (weapons .activeInHierarchy) {
weapAmmos = weapons .GetComponent ().nameWeap;
* selectChildren [2].GetComponent ().text = GameManagerScript.GetInstance ().GetAmmo (weapAmmos).ToString ();
}
}
}*_

* void AddEvent(Button[] buttons, Button button, string weapon, GameObject[] weapons, Transform select, Transform textWeapon) {*
* button.gameObject.AddComponent(typeof(EventTrigger));*
* EventTrigger trigger = button.gameObject.GetComponent();*
* EventTrigger.Entry entry = new EventTrigger.Entry();*
* entry.eventID = EventTriggerType.PointerClick;*
* entry.callback.AddListener( (eventData) => {*

* string weapToInsert = null;*

* for (int i = 0; i < weapons.Length; i++) {*
_ if (weapon != weapons*.name) {
weapons.SetActive(false);
}
else {
weapons.SetActive(true);
select.GetComponent ().sprite = weapons.GetComponent().img;
weapToInsert = weapons.GetComponent().nameWeap;
}
}*_

* for (int j = 0; j < buttons.Length; j++) {*

* Transform[] selector = buttons[j].GetComponentsInChildren (true);*

* if (button.name != buttons[j].name)*
* selector[2].gameObject.SetActive(false);*
* else*
* selector[2].gameObject.SetActive(true);*
* textWeapon.GetComponent().text = GameManagerScript.GetInstance ().GetAmmo (weapToInsert).ToString();*
* }*
* });*
* trigger.triggers.Add(entry);*
* }*
}