- Home /
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?
Do you have a gameobject in your scene with the tag "WeaponShoot"? It looks like the script can't find one in the start function.
Answer by Rizzutp · May 07, 2018 at 09:44 AM
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 [i].GetComponentsInChildren<Transform> ();
if (i == 0) {
weapons [i].SetActive (true);
childrenButt [2].gameObject.SetActive (true);
}
else {
weapons [i].SetActive (false);
childrenButt [2].gameObject.SetActive (false);
}
childrenButt [1].GetComponent<Image> ().sprite = weapons [i].GetComponent<GenericWeapon> ().img;
AddEvent (butts, butts [i], weapons [i].name, weapons, selectChildren[1], selectChildren[2]);
}
}
void Update () {
for (int i = 0; i < weapons.Length; i++) {
string weapAmmos;
if (weapons [i].activeInHierarchy) {
weapAmmos = weapons [i].GetComponent<GenericWeapon> ().nameWeap;
selectChildren [2].GetComponent<TextMeshProUGUI> ().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>();
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[i].name) {
weapons[i].SetActive(false);
}
else {
weapons[i].SetActive(true);
select.GetComponent<Image> ().sprite = weapons[i].GetComponent<GenericWeapon>().img;
weapToInsert = weapons[i].GetComponent<GenericWeapon>().nameWeap;
}
}
for (int j = 0; j < buttons.Length; j++) {
Transform[] selector = buttons[j].GetComponentsInChildren<Transform> (true);
if (button.name != buttons[j].name)
selector[2].gameObject.SetActive(false);
else
selector[2].gameObject.SetActive(true);
textWeapon.GetComponent<TextMeshProUGUI>().text = GameManagerScript.GetInstance ().GetAmmo (weapToInsert).ToString();
}
});
trigger.triggers.Add(entry);
}
}
Answer by Carterryan1990 · Apr 19, 2018 at 02:56 AM
Shoot isnt being found with the tag. Make sure your shoot gameobject has the proper tag.
Answer by TauseefCVS · Apr 19, 2018 at 03:23 AM
you this code instead of your code
public void someFunction()
{
weapon=!weapon
if(weapon)
shoot.SetActive (true);
else
jump.SetActive (false);
}
Sorry, but I don't get the "weapon=!weapon" statement. What do you mean with the term "weapon" within your code?
Ok, I tried it with a boolean value called "weapon", but it gives the same exact error