Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Search and get parameters of a sub-gameobject in Canvas

Discussion in 'Scripting' started by MedalHellWay, Sep 22, 2018.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Hi all :)

    Honestly I did not want to open this discussion, but I searched the whole forum and did not find anything that would really help me. The nproblem is:

    I've a empty gameobject that which contains multiple canvases (image below) and it turn on and off the whole group:

    Test.jpg

    Now ignoring how to get to take and use a component, I have the problem of using a sub-object such as the test highlighted in the image. Practical example:

    - I've a Cube object with with an attached script that contains an explanation for the cube itself
    - I've a Player that collide with the Cube and active the script/cube
    - With GameObject.Find metod, I check the sub-gameobject

    Code (CSharp):
    1. textHelp = GameObject.Find("ItemHelp_Text").GetComponent<Text>();
    Now the problem is born, I can't associate the cube description script with "ItemHelp_Text" canvas, or I can't get into its parameters :(

    Where am I wrong? What to do? what did I miss? I've been trying for a few days and I think I've "wrapped up" :D

    EDIT: Sorry for double post, but I've forgot a second question: The cube script that contains the text is inserted into a function. When I call it I've the error that does not allow me to transform the void in string. In this case how to act?
     
    Last edited: Sep 22, 2018
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    I'm not myself really good in English but I think you're not very understandable here.

    Whare are you trying to do?

    Get the UI Text component of a specific gameobject and access its variables and methods?
     
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Yes, but above all enter the sub-levels and access the parameters etc (you see the image attach. In other circumstances normally do not have problems accessing the parameters access, but in the above example I do not understand..
     
  4. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Sorry for the UP, but nothing tip from community?
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @MedalHellWay

    The question is really vague, maybe you could rephrase it so it's more understandable.

    There is nothing wrong with how you get your Text component, what else are you trying to do?

    What do you mean by:

    "I can't associate the cube description script with "ItemHelp_Text" canvas, or I can't get into its parameters"

    Are you trying to get the canvas where ItemHelp_Text is or what do you mean?

    To change text of ItemHelp_Text, just do:

    Code (CSharp):
    1. textHelp.text = myTextData;
    To get some ui element's parent canvas do something like this:

    Code (CSharp):
    1. myParentCanvas = this.GetComponentInParent<Canvas>();    
    But I have no idea what you are trying to do.
     
    MedalHellWay likes this.
  6. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, I try to explain myself better. Sorry if I've not been clear :(

    I've a Cube with an attached script that describes it. When I collide him with my Player, the description must appear in the canvas.
    So to improve performance in Unity, I have used more canvas for every use I've to make and grouped them under an Empty GameObject. The image below shows the result:

    hit_01.jpg

    Now, I've no problem show the text in video during the test, but when I try to transfer it to Canvas, I have problems accessing its components, and I can't understand out where I'm wrong.

    So with .Find method I can access the text in the canvas...

    Code (CSharp):
    1. textHelp = GameObject.Find("ItemHelp_Text").GetComponent<Text>();
    but with
    Code (CSharp):
    1.  textHelp.text
    I can not log in the canvas text. Surely I'm wrong somewhere, but I can not understand.. :(

    I hope I have been clearer and If you want, I have attached the test scene to make you understand how I am operating ...

    EDIT: there is a bit of confusion in the test scene, but it's my way of working unfortunately !!
     

    Attached Files:

  7. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You create a unstable solution if you use the scene hierarchy to communicate between domains. It's better to use some kind of event bus and or command bus.
     
    MedalHellWay likes this.
  8. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    So, directly deduct the gameobject in the Inspector? ...or what do you mean exactly?
     
  9. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Build your own structure ontop of the gameobjects that does not rely on their structure at all, except maybe close to the metal.
     
    MedalHellWay likes this.
  10. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks for quick reply :) I thought to use an empty gameobject to turn off and turn on all the canvas quickly ... so it's wrong?
     
    Last edited: Sep 23, 2018
  11. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    No, it's not wrong but how depend on it can be less optimal
     
    MedalHellWay likes this.
  12. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks for the tip :)