- Home /
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);
}
}
}
}
}
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);
}
}
}
}
Your answer
Follow this Question
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