ALAC3
January 1, 2017, 5:06am
1
I’m working on my game called ORA and basically what I’m trying to do is Find an object’s child but it wont work for some reason? Here’s the code.
public Image ItemImage;
public Image ItemImage1;
public Image ItemImage2;
public Image ItemImage3;
public Image ItemImage4;
public Image WeaponImage;
public Image ToolImage;
public RectTransform InventorySlots;
public Image BlueW;
public Image BlueT;
public Image ItemDescriptionPanel;
public Text ItemDescription;
public Text InPickUpDistance;
public Text Name;
public Text Level;
public Text Power;
public Text Damage;
public Text ManaCost;
public Text Range;
public Text Defense;
public Text Craft;
public Text PlayerDefense;
public Text PlayerLv;
public Slider Exp;
public Slider healthBar;
public Slider hungerBar;
public Slider thirstBar;
public Slider manaBar;
public GameObject UI;
public Canvas InventoryUI;
DeathCanvas = GameObject.Find(“DeathCanvas”);
InventoryUI = GameObject.Find(“InventoryCanvas”).GetComponent();
UI = GameObject.Find(“UICanvas”);
healthBar = UI.transform.Find("PlayerHealthBar").GetComponent<Slider>();
hungerBar = UI.transform.Find("PlayerHungerBar").GetComponent<Slider>();
thirstBar = UI.transform.Find("PlayerThirstBar").GetComponent<Slider>();
manaBar = UI.transform.Find("PlayerManaBar").GetComponent<Slider>();
PlayerLv = UI.transform.Find("PlayerLv").GetComponent<Text>();
Exp = PlayerLv.transform.Find("PlayerExp").GetComponent<Slider>();
PlayerDefense = UI.transform.Find("PlayerDefense").GetComponent<Text>();
ItemDescriptionPanel = UI.transform.Find("ItemDescriptionPanel").GetComponent<Image>();
ItemDescription = UI.transform.Find("ItemDescription").GetComponent<Text>();
Craft = ItemDescription.transform.Find("ItemCraft").GetComponent<Text>();
Defense = ItemDescription.transform.Find("ItemDefense").GetComponent<Text>();
Range = ItemDescription.transform.Find("ItemRange").GetComponent<Text>();
ManaCost = ItemDescription.transform.Find("ItemManaCost").GetComponent<Text>();
Damage = ItemDescription.transform.Find("ItemDamage").GetComponent<Text>();
Power = ItemDescription.transform.Find("ItemPower").GetComponent<Text>();
Level = ItemDescription.transform.Find("ItemLevel").GetComponent<Text>();
Name = ItemDescription.transform.Find("ItemName").GetComponent<Text>();
ToolImage = InventoryUI.transform.Find("ToolImage").GetComponent<Image>();
BlueT = InventoryUI.transform.Find("BlueT").GetComponent<Image>();
BlueW = InventoryUI.transform.Find("BlueW").GetComponent<Image>();
WeaponImage = InventoryUI.transform.Find("WeaponImage").GetComponent<Image>();
ItemImage = InventoryUI.transform.Find("ItemImage").GetComponent<Image>();
InventorySlots = InventoryUI.transform.Find("PlayerInventorySlots").GetComponent<RectTransform>();
ItemImage1 = InventorySlots.transform.Find("ItemSlots1").GetComponent<Image>();
ItemImage2 = InventorySlots.transform.Find("ItemSlots2").GetComponent<Image>();
ItemImage3 = InventorySlots.transform.Find("ItemSlots3").GetComponent<Image>();
ItemImage4 = InventorySlots.transform.Find("ItemSlots4").GetComponent<Image>();
Your code seems totally fine,
perhaps, don’t use UI as the variable name, since UI is a unity’s namespace
Make sure the script is attached to gameObject. Check in hierarchy that the children are present
ALAC3
January 1, 2017, 10:10pm
3
After all the tears and keyboard stumping I found a way to do it!!! I know its going to be someone else just like me so I’m going to show you the code here you go!!!.
So first I made everything that had to do with UI a GameObject because I couldn’t find any Object unless i did this. Then I converted everything to know that the object was a ui. Sorry its hard for me to explain. After that I watched as my soon to be multiplayer game could put any character in the seen and not act retarded. Also sorry for so much code all of it matters . IM SO PROUD OF MY SELF THIS TOOK ME 3 DAYS!
public GameObject BlueW;
public GameObject BlueT;
public GameObject ItemDescriptionPanel;
public GameObject ItemDescription;
public GameObject InPickUpDistance;
public GameObject Name;
public GameObject Level;
public GameObject Power;
public GameObject Damage;
public GameObject ManaCost;
public GameObject Range;
public GameObject Defense;
public GameObject Craft;
public GameObject PlayerDefense;
public GameObject PlayerLv;
public GameObject ItemImage;
public GameObject ItemImage1;
public GameObject ItemImage2;
public GameObject ItemImage3;
public GameObject ItemImage4;
public GameObject WeaponImage;
public GameObject ToolImage;
public GameObject InventorySlots;
public GameObject Exp;
public GameObject healthBar;
public GameObject hungerBar;
public GameObject thirstBar;
public GameObject manaBar;
public GameObject UI;
public Canvas InventoryUI;
void Start () {
//ItemDescription.GetComponentInChildren ();
DeathCanvas = GameObject.Find("DeathCanvas");
InventoryUI = GameObject.Find("InventoryCanvas").GetComponent<Canvas>();
UI = GameObject.Find("UICanvas");
InPickUpDistance = GameObject.Find("ItemPickUpSign");
healthBar = GameObject.Find("Health");
hungerBar = GameObject.Find("Food");
thirstBar = GameObject.Find("Thirst");
manaBar = GameObject.Find("PlayerManaBar");
PlayerLv = GameObject.Find("PlayerLv");
Exp = GameObject.Find("PlayerExp");
PlayerDefense = GameObject.Find("PlayerDefense");
ItemDescriptionPanel = GameObject.Find("ItemDescriptionPanel");
ItemDescription = GameObject.Find("PlayerItemDescription");
Craft = GameObject.Find("ItemCraft");
Defense = GameObject.Find("ItemDefense");
Range = GameObject.Find("ItemRange");
ManaCost = GameObject.Find("ItemManaCost");
Damage = GameObject.Find("ItemDamage");
Power = GameObject.Find("ItemPower");
Level = GameObject.Find("ItemLevel");
Name = GameObject.Find("ItemName");
ToolImage = GameObject.Find("ToolImage");
BlueT = GameObject.Find("BlueT");
BlueW = GameObject.Find("BlueW");
WeaponImage = GameObject.Find("WeaponImage");
ItemImage = GameObject.Find("ItemImage");
InventorySlots = GameObject.Find("PlayerInventorySlots");
ItemImage1 = GameObject.Find("ItemSlot1");
ItemImage2 = GameObject.Find("ItemSlot2");
ItemImage3 = GameObject.Find("ItemSlot3");
ItemImage4 = GameObject.Find("ItemSlot4");
void FixedUpdate()
{
PlayerDefense.GetComponent<Text>().text = "Defense " + Armor;
if (ItemDescription.GetComponent<Text>().enabled == true)
{
ItemDescriptionPanel.GetComponent<Image>().enabled = true;
}else
{
if (ItemDescription.GetComponent<Text>().enabled == false)
{
ItemDescriptionPanel.GetComponent<Image>().enabled = false;
}
}
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, 3))
{
IObject ItemObject = _hit.collider.GetComponent<IObject>();
if (ItemObject)
{
InPickUpDistance.GetComponent<Text>().enabled = true;
ItemDescription.GetComponent<Text>().enabled = true;
ItemDescription.GetComponent<Text>().text = ItemObject.ItemDescription;
Name.GetComponent<Text>().enabled = true;
Name.GetComponent<Text>().text = ItemObject.ItemName;
if (ItemObject.CanCraft == true)
{
Craft.GetComponent<Text>().enabled = true;
if (Input.GetKeyDown(KeyCode.C))
{
Debug.Log("PRESSED");
ItemObject.TheOneToCraft = true;
}
}
if (ItemObject.GetComponent<MagicWeapon>())
{
ManaCost.GetComponent<Text>().enabled = true;
Level.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = false;
Power.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
ManaCost.GetComponent<Text>().text = "ManaCost " + ItemObject.GetComponent<MagicWeapon>().ManaCost;
//Range.text = "Range " + ItemObject.GetComponent<Gun>().WeaponRange;
Damage.GetComponent<Text>().enabled = true;
Damage.GetComponent<Text>().text = "Damage " + ItemObject.GetComponent<MagicWeapon>().Damage;
}else
{
Range.GetComponent<Text>().text = null;
}
if (ItemObject.GetComponent<PWeapon>())
{
ManaCost.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = false;
Power.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = true;
Level.GetComponent<Text>().text = "Level " + ItemObject.GetComponent<PWeapon>().level;
Damage.GetComponent<Text>().enabled = true;
Damage.GetComponent<Text>().text = "Damage " + ItemObject.GetComponent<PWeapon>().damage;
}else
{
if (ItemObject.GetComponent<MagicWeapon>())
{
}
else
{
Damage.GetComponent<Text>().text = null;
}
Level.GetComponent<Text>().text = null;
}
if (ItemObject.GetComponent<PTool>())
{
ManaCost.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = true;
Level.GetComponent<Text>().text = "Level " + ItemObject.GetComponent<PTool>().level;
Power.GetComponent<Text>().enabled = true;
Power.GetComponent<Text>().text = "Power " + ItemObject.GetComponent<PTool>().power;
}else
{
Power.GetComponent<Text>().text = null;
}
if (ItemObject.GetComponent<Helmet>())
{
ManaCost.GetComponent<Text>().enabled = false;
Power.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = true;
Defense.GetComponent<Text>().text = "Defense " + ItemObject.GetComponent<Helmet>().defense;
}
if (ItemObject.GetComponent<ChestPlate>())
{
Power.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = true;
Defense.GetComponent<Text>().text = "Defense " + ItemObject.GetComponent<ChestPlate>().defense;
}
if (ItemObject.GetComponent<Leggings>())
{
Power.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = true;
Defense.GetComponent<Text>().text = "Defense " + ItemObject.GetComponent<Leggings>().defense;
}
if (ItemObject.GetComponent<Boots>())
{
Power.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = true;
Defense.GetComponent<Text>().text = "Defense " + ItemObject.GetComponent<Boots>().defense;
}
}
else { ItemDescription.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
Name.GetComponent<Text>().enabled = false;
InPickUpDistance.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Power.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = false;
ItemDescription.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Craft.GetComponent<Text>().enabled = false;
ManaCost.GetComponent<Text>().enabled = false;
}
}else {
ManaCost.GetComponent<Text>().enabled = false;
Craft.GetComponent<Text>().enabled = false;
Range.GetComponent<Text>().enabled = false;
Damage.GetComponent<Text>().enabled = false;
Power.GetComponent<Text>().enabled = false;
Defense.GetComponent<Text>().enabled = false;
InPickUpDistance.GetComponent<Text>().enabled = false;
Name.GetComponent<Text>().enabled = false;
Level.GetComponent<Text>().enabled = false;
ItemDescription.GetComponent<Text>().enabled = false; }
}
void Update () {
if (Input.GetKey(KeyCode.G))
{
InventorySlots.gameObject.SetActive(true);
}else { InventorySlots.gameObject.SetActive(false); }
Exp.GetComponent<Slider>().maxValue = XpForNextLv;
if (Xp >= XpForNextLv)
{
Lv++;
Xp = 0;
XpForNextLv += 40;
}
PlayerLv.GetComponent<Text>().text = "Lv " + Lv;
Exp.GetComponent<Slider>().value = Xp;
manaBar.GetComponent<Slider>().value = Mana;
healthBar.GetComponent<Slider>().value = health;
thirstBar.GetComponent<Slider>().value = thirst;
hungerBar.GetComponent<Slider>().value = hunger;
float move = Input.GetAxisRaw("Vertical");
Animator.SetFloat("Walk", move);
Animator.SetBool("Walking", Walking);
Animator.SetBool("Run", Running);
if(GetComponent<PlayerInven>().UsingTool == true)
{
BlueT.GetComponent<Image>().enabled = true;
}else {
BlueT.GetComponent<Image>().enabled = false;
}
if (GetComponent<PlayerInven>().UsingWeap == true)
{
BlueW.GetComponent<Image>().enabled = true;
}
else { BlueW.GetComponent<Image>().enabled = false; }
if (move == 0f)
{
Walking = false;
}else { Walking = true; }
if(move >= 1&& Input.GetKey(KeyCode.LeftShift))
{
Running = true;
}else { Running = false; }
if (GetComponent<PlayerInven>().Tool != null)
{
ToolImage.GetComponent<Image>().enabled = true;
ToolImage.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Tool.GetComponent<IObject>().InventoryImage;
}else { ToolImage.GetComponent<Image>().enabled = false; }
if(GetComponent<PlayerInven>().Weapon != null)
{
WeaponImage.GetComponent<Image>().enabled = true;
WeaponImage.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Weapon.GetComponent<IObject>().InventoryImage;
}else
{
if (GetComponent<PlayerInven>().MagicWeapon == null)
{
WeaponImage.GetComponent<Image>().enabled = false;
}
}
if (GetComponent<PlayerInven>().MagicWeapon != null)
{
WeaponImage.GetComponent<Image>().enabled = true;
WeaponImage.GetComponent<Image>().sprite = GetComponent<PlayerInven>().MagicWeapon.GetComponent<IObject>().InventoryImage;
}
else
{
if (GetComponent<PlayerInven>().Weapon == null)
{
WeaponImage.GetComponent<Image>().enabled = false;
}
}
if (GetComponent<PlayerInven>().Item != null)
{
ItemImage.GetComponent<Image>().enabled = true;
ItemImage.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Item.InventoryImage;
}else { ItemImage.GetComponent<Image>().enabled = false; }
if (GetComponent<PlayerInven>().Item1 != null)
{
ItemImage1.GetComponent<Image>().enabled = true;
ItemImage1.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Item1.InventoryImage;
}
else { ItemImage1.GetComponent<Image>().enabled = false; }
if (GetComponent<PlayerInven>().Item2 != null)
{
ItemImage2.GetComponent<Image>().enabled = true;
ItemImage2.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Item2.InventoryImage;
}
else { ItemImage2.GetComponent<Image>().enabled = false; }
if (GetComponent<PlayerInven>().Item3 != null)
{
ItemImage3.GetComponent<Image>().enabled = true;
ItemImage3.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Item3.InventoryImage;
}
else { ItemImage3.GetComponent<Image>().enabled = false; }
if (GetComponent<PlayerInven>().Item4 != null)
{
ItemImage4.GetComponent<Image>().enabled = true;
ItemImage4.GetComponent<Image>().sprite = GetComponent<PlayerInven>().Item4.InventoryImage;
}
else { ItemImage4.GetComponent<Image>().enabled = false; }