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 Levithan6785 · Apr 08, 2015 at 09:23 PM · stringgameobject.find

GameObject.Find object with Variable string C#

I'm currently working on a inventory system for future reference for when I really start making a game. But I've run into a little problem.

                     GameObject equipped = GameObject.Find (item));
                     equipped.SetActive (true);

The variable "item" is a string and in my code higher up, the string item is given a string when the player picks up an item. setting "item = Cloak of Wonders". (Though it is not shown to make this question short). Now, when the player hits the equip button after picking up the item, I want to be able to find the gameobject with the same string as "item" and set it's active to true, which in turn activates the deactivated gameobject and applying the stat changes and appearance. I have a script that works when equipping the item directly from the ground, which I use this to activate.

                     itemObj.gameObject.SetActive(true);

The variable "itemObj" is assigned a gameobject in the hierarchy. But I simply can't do the same thing without making the script unnecessarily long with a bunch of if statements. I am curious to know if there is a simpler way to do it then writing a bunch of if statements which can make the script take a little bit longer to execute. If there isn't another way, then oh well.

-Thank you for anyone who provides an answer to my small dilemma

(Full Script with "item" variable.):

 using UnityEngine;
 using System.Collections;
 
 public class PlayerInventory : MonoBehaviour 
 {
     public int health;
     public int damage;
     public int resist;
 
     public int itemNum;
     public string item;
 
     private int inventory;
     private bool equip;
 
     void Update()
     {
         if (Input.GetKeyDown ("e"))
         {
             if (inventory == 1)
             {
                 inventory = 0;
             }
             else
             {
                 inventory = 1;
                 Additem();
             }
         }
     }
 
     void Additem()
     {
         if (itemNum == 0)
         {
             item = "";
         }
         else if (itemNum == 1)
         {
             item = "Cloak of Wonders";
         }
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect (10,10,300,300), health.ToString());
         GUI.Label (new Rect (10,30,300,300), damage.ToString());
         GUI.Label (new Rect (10, 50, 300, 300), resist.ToString());
 
         if (inventory == 1)
         {
             if (GUI.Button (new Rect (Screen.width/2-100, 300, 200, 50), item))
             {
                 equip = true;
             }
         }
 
         if (equip == true)
         {
             if (item == "")
             {
                 //Do nothing
             }
             else
             {
                 if ( GUI.Button (new Rect (550,30,100,40), "Equip"))
                 {
                     GameObject equipped = GameObject.Find (item);
                     equipped.gameObject.SetActive (true);
                 }
             }
         }
     }
 
 }
 
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 NoseKills · Apr 08, 2015 at 09:24 PM 0
Share

The problem is GameObject.Find can't find inactive objects. You have to put the objects you want to reference/activate into some other kind of structure (array/List/Dictionary) and activate them from there when needed.

A la:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : $$anonymous$$onoBehaviour {
 
     private Dictionary<string, GameObject> _Inventory;
     void Start ()
     {
         _Inventory = new Dictionary<string, GameObject>();
         GameObject[] allItemsInScene = GameObject.FindGameObjectsWithTag("inventoryItem");
         foreach (GameObject item in allItemsInScene)
         {
                     item.SetActive(false);
             _Inventory.Add(item.name, item);
         }
     }
 
     ...

     void OnGUI()
     {

     ...

         if ( GUI.Button (new Rect (550,30,100,40), "Equip"))
         {
             GameObject equipped = null;
             if (_Inventory.TryGetValue(item, out equipped)
             {
                 equipped.gameObject.SetActive (true);
             }
         }
     }
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to get game object by string? 0 Answers

Clones not being Destroyed 1 Answer

GameObject.Find(TextFieldVariable) - Doesnt seem to work! 1 Answer

replacing a character in a String 1 Answer

returning a variable in javascript 1 Answer


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