Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rizzutp · Apr 18, 2018 at 11:25 PM · buttongameobjectsonclickenable and disable script

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Serinx · Apr 19, 2018 at 01:24 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · Apr 19, 2018 at 08:29 AM 0
Share

The tag is already set properly.

avatar image
0

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);
}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · Apr 19, 2018 at 08:34 AM 0
Share

Sorry, but I don't get the "weapon=!weapon" statement. What do you mean with the term "weapon" within your code?

avatar image Rizzutp · Apr 19, 2018 at 08:41 AM 0
Share

Ok, I tried it with a boolean value called "weapon", but it gives the same exact error

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Method added on a button click with a delegate is being called twice. 0 Answers

Unity 5.0.2f1 UI Button OnClick Function 6 Answers

UI Button OnClick Function - How to get name of button that was clicked? 2 Answers

How to begin startcoroutine (IEnumerator) with UI button. 1 Answer

How to trigger a button click from script 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges