Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bonkahe · Oct 31, 2013 at 04:09 PM · guibuttonarrayinventory

Click on a button that is created post start.

Basically I have buttons on a gui that are only created After you start the game (to be specific when you pick up items) and I want it so when you right click that object in the inventory it does something (Later on it will have a menu to eat drop w/e) but I can't seem to wrap my head around it.

Here is the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class BaseGirlControl : MonoBehaviour {
     // Health & Heat
     
     public int maxhealth = 100;
     public int curhealth = 100;
     private float healthbarlength;
     
     public int maxheat = 100;
     public int curheat = 100;
     private float heatbarlength;
     
     public Transform GameController;
     
     private bool rendermenu;
     
     
     // movement
     
     public float animSpeed = 1.5f;
     public float speed = 10.0f;
     public float RotationSpeed = 100;
     private Animator anim;
     
     // in hand items
     
     public string pickupTag;
     private Transform pickuploc;
     private Transform rightPalmloc;
     private bool RightHandEquip;
     public bool collideweapon;
     
     // Non in-hand items
     
     public List<string> NearbyItems = new List<string>();
     
     private RaycastHit hit;
     private Ray ray;
     private Transform ItemHolder;
     
     public GameObject[] InventorySlots = new GameObject[6];
     private Rect windowRect = new Rect(10, 300, 104, 450);
     
     public Texture2D DefaultItemIcon;
     
     // Use this for initialization
     void Start () {
         pickupTag = null;
         anim = GetComponent<Animator>();    
         RightHandEquip = false;
         collideweapon = false;
     }
 
     
     // Update is called once per frame
     void Update () {
         HandlesGUI hg = (HandlesGUI)GameController.GetComponent("HandlesGUI");
         if (hg.Rendermenu == true)
         {
             rendermenu = true;
         }
         else
         {
             rendermenu = false;
         }
         AddjustCurrentHealth(0);
         AddjustCurrentHeat(0);
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         anim.speed = animSpeed;
         if(rendermenu == false)
         {
             anim.SetFloat("Speedz", v);            
             anim.SetFloat("Speedx", h);
             transform.Rotate(0, Input.GetAxis("Mouse X"), 0 * Time.deltaTime * speed);
         }
         if(rendermenu == true)
         {
             anim.SetFloat("Speedz", 0);            
             anim.SetFloat("Speedx", 0);
         }
 
         
         
         
         // Handles weapons / In hand items
         
         
         if (Input.GetButton("InteractionKey"))
         {
             if (collideweapon == true)
             {
                 if (pickupTag != null)
                 {
                     pickuploc = GameObject.FindWithTag(pickupTag).transform;
                     pickuploc.GetComponent<WeaponControl>().ispickedup = true;
                     rightPalmloc = GameObject.FindWithTag("GrippingHand").transform;
                     pickuploc.transform.parent = rightPalmloc.transform;
                     RightHandEquip = true;
                 }
             }
         }
         if (Input.GetButton("DropWeaponKey"))
         {
             if (RightHandEquip == true)
             {
                 pickuploc = GameObject.FindWithTag(pickupTag).transform;
                 pickuploc.GetComponent<WeaponControl>().ispickedup = false;
                 RightHandEquip = false;
             }
         }
         
         // Handles Clicking in gui mode to pick up items
         
         if(rendermenu == true)
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Input.GetButtonDown("Fire1"))
             {
                 if(Physics.Raycast(ray,out hit,20))
                 {
                     if(NearbyItems.Contains(hit.collider.tag))
                     {
                         for(int i = 0; i < InventorySlots.Length; i++)
                         {
                             if(InventorySlots[i] == null)
                             {
                                 ItemControl item = (ItemControl)hit.collider.gameObject.GetComponent<ItemControl>();
                                 InventorySlots[i] = hit.collider.gameObject;
                                 item.ispickedup = true;
                                 ItemHolder = GameObject.FindWithTag(item.SlotTag).transform;
                                 InventorySlots[i].transform.parent = ItemHolder.transform;
                                 break;
                             }
                             else
                             {
                                 Debug.Log("YO INVENTORY FULL SON!");
                             }
                         }
                     }
                 }    
             }
         }
     }
     void OnGUI ()
     {
         // Health system
         if (rendermenu == true)
         {
             GUI.Box(new Rect(10, 10, 120, 20), "Health");
             GUI.Box(new Rect(10, 30, healthbarlength, 20), curhealth + "/" + maxhealth);
             
             GUI.Box(new Rect(10, 50, 120, 20), "Heat");
             GUI.Box(new Rect(10, 70, heatbarlength, 20), curheat + "/" + maxheat);
             
             // Calls Inventory GUI
             
             windowRect = GUI.Window(0, windowRect, Domywindow, "Inventory" );
         }
     }
     
     // Health system
     
     public void AddjustCurrentHealth(int adj) {
         curhealth += adj;
     
         if(curhealth < 0)
             curhealth = 0;
         
         if(curhealth > maxhealth)
             curhealth = maxhealth;
         
         if(maxhealth < 1)
             maxhealth = 1;
         
         healthbarlength = (Screen.width / 2) * (curhealth / (float)maxhealth);
         
         if(curhealth == 0)
             Application.LoadLevel("MainMenu");
     }
     
     public void AddjustCurrentHeat(int adjh) {
         curheat += adjh;
     
         if(curheat < 0)
             curheat = 0;
         
         if(curheat > maxheat)
             curheat = maxheat;
         
         if(maxheat < 1)
             maxheat = 1;
         
         heatbarlength = (Screen.width / 2) * (curheat / (float)maxheat);
         
         if(curheat == 0)
             Debug.Log("Heatcold");
     }
     
     // Inventory Gui
     
     void Domywindow(int windowId)
     {
         int y = 20;
         GUI.DragWindow(new Rect(0,0,300,20));
         for(int i = 0; i < InventorySlots.Length; i++)
         {
             if(InventorySlots[i] != null)
             {
                 ItemControl v1c = (ItemControl)InventorySlots[i].gameObject.GetComponent<ItemControl>();
                 if(InventorySlots[i].tag != null)
                 {
                     GUI.Button(new Rect(20,y,64,64),v1c.icon);
                     y += 70;
                 }
             }
             else
             {
                 GUI.Button(new Rect(20,y,64,64),DefaultItemIcon);
                 y += 70;
             }
         }
         
     }
 }


Don't think you'll be needing this but here is the item script:

     public bool ispickedup;
     public Texture2D icon;
     public GameObject Player;
     public string SlotTag;
     
     // Use this for initialization
     void Start () {
         ispickedup = false;
     }
     
     void OnTriggerEnter(Collider collide) 
     {
         if (collide.gameObject.tag == "Player")
         {
             BaseGirlControl bgc = (BaseGirlControl)Player.GetComponent("BaseGirlControl");
             if(bgc.NearbyItems.Contains(tag))
             {
             }
             else
             {
                 bgc.NearbyItems.Add(tag);
             }
         }
     }
     
     void OnTriggerExit(Collider collide) 
     {
         if (collide.gameObject.tag == "Player")
         {
             BaseGirlControl bgc = (BaseGirlControl)Player.GetComponent("BaseGirlControl");
             if(bgc.NearbyItems.Contains(tag))
             {
                 bgc.NearbyItems.Remove(tag);
             }
         }
     }
     
     
     // Update is called once per frame
     void Update () {
         if (ispickedup == true)
         {
             Held();
         }
         
         if (ispickedup == false)
         {
             Drop();
         }
     }
     
     void Held()
     {
         SphereCollider sc = GetComponent<SphereCollider>();
         BoxCollider bc = GetComponent<BoxCollider>();
         transform.localPosition = new Vector3(0, 0, 0);
         transform.localRotation = Quaternion.identity;
         rigidbody.Sleep();
         sc.enabled = false;
         bc.enabled = false;
         gameObject.renderer.enabled = false;
     }
     
     void Drop()
     {
         SphereCollider sc = GetComponent<SphereCollider>();
         BoxCollider bc = GetComponent<BoxCollider>();
         rigidbody.WakeUp();
         sc.enabled = true;
         bc.enabled = true;
         gameObject.renderer.enabled = true;
     }
 }


Thoughts?

I'm off to work, be back later.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

Text pop up when mouse over gui button 2 Answers

GUI.Button is acting funky. 2 Answers

GUI Button Position - Can it float to the top-left? 3 Answers

Creating GUI Buttons with a for loop from an array 6 Answers

GUI Button with both texture and text? 4 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