Search Unity

addressing mesh renderer of sub-item

Discussion in 'Getting Started' started by matzomat, Mar 10, 2018.

  1. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    Hey guys,

    a total noob question I guess.

    Setup:
    empty GameObject called "unitPlayer1" with the usual colliders and stuff. Attached to it is the working model and a "Quad" with the name "Selector". I want to show the selector when the unit is selected and hide it, when it's unselected through renderer.enabled. I have a script attached to the emptyGameObject for handling the clicks and it works properly, I confirmed that through debug logs.

    My problem is that I have no clue how to find the mesh of the Selector from within that script.
            rend = Selector.GetComponent<Renderer>();

    is not working but I want to address the selector specifically and not go through all children. How can I do that?

    Thank you in advance.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    To do that, you would need to make a public property of type Renderer on your script. Then, while inspecting the object in your scene with the script on it, grab the Quad from the Hierarchy tab and drag it into this new property on your script.

    Then just use that property in your code.
     
    matzomat likes this.
  3. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    Thank you for your answer. You mean like:
    public transform renderer;
    ?

    In the meanwhile I'm helping myself with this but I don't like it because another "Selector" in some model might cause problems.
    transform.FindChild("Selector").gameObject.SetActive(true);
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Not quite. It should be:
    public Renderer selector;
     
    matzomat likes this.
  5. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    Thank you!

    successfully implemented by:

    public Renderer selectorAnim;
    ...
    selectorAnim.gameObject.SetActive(false);


    Have a nice weekend.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well done!

    Note that I thought you wanted to enable/disable the Renderer component (selectorAnim.enabled = false), not the whole GameObject. If you want to activate/deactivate the whole GameObject, then you might consider changing the type of selectorAnim to GameObject, to better reflect what it's doing and allow you to remove the .gameObject part of your code.
     
  7. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    hm... actually I think it would be cleaner to deactivate the whole object. Having tons of objects in the world that simply aren't drawn but still exist sound kind of goofy to me.

    Thx for the hint!
     
  8. Dai-22

    Dai-22

    Joined:
    Jan 19, 2018
    Posts:
    130
    I was wondering, on the issue of efficiency, is de-activate or destroy better for game speed etc?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It certainly depends on the detalis of the situation. Beginners should not worry about such things. They greatest risk to your project is that you will never finish it. So always code in whatever way seems simplest to you, so that (A) there's a better chance of avoiding bugs, and (B) a better chance you'll actually finish the project.