- Home /
Finding a panel in canvas
//find CharacterInfoPanel
CharacterInfoPanel = GameObject.Find("Canvas").GetComponent("CharacterInfoPanel");
I have been watching a tutorial from 3 years ago and this worked there but I doesn't like it now, any idea how to make it work?
error CS0029: Cannot implicitly convert type UnityEngine.Component' to
UnityEngine.GameObject'
Answer by fafase · Dec 29, 2018 at 03:46 PM
It appears CharacterInfoPanel is of type GameObject which is why the compiler complains it cannot store a Component into this.
So either you want to modify CharacterInfoPanel to be of type CharacterInfoPanel and then you'd need a cast (I would also rename it a bit)
CharacterInfoPanel characterInfoPanel;
characterInfoPanel = GameObject.Find("Canvas").GetComponent("CharacterInfoPanel") as CharacterInfoPanel ;
You need the cast since GetComponent(string) returns a Component type object, even though you passed a more defined type. You could use the generic version but there may be a reason why you need the string version. If you don't need the string go as:
CharacterInfoPanel characterInfoPanel;
characterInfoPanel = GameObject.Find("Canvas").GetComponent<CharacterInfoPanel>();
Last case, it means you are not after the component but only the GameObject holding the component (I think you want solution 1 though):
GameObject characterInfoPanel;
characterInfoPanel = GameObject.Find("Canvas");
I did renamed CharacterInfoPanel to CharacterBar as it wasn't actually correct. And I think it is a GameObject I mean it's an Image. I want to instantiate into a canvas panel. So I have a Canvas named Canvas, and a panel attached named CharacterInfoPanel, it's to this panel I want to attacthed CharacterBar which is a Unity UI image. And it does instantiate it just fine now, but It does it outside the panel
public GameObject CharacterBar;
private GameObject CharacterInfoPanel;
void Start ()
{
//find CharacterInfoPanel
CharacterBar = GameObject.Find("CharacterBar");
//create a panel and fill the info of the character (GUI)
CreateCharacterInfoPanel();
void CreateCharacterInfoPanel()
{
CharacterBar = Instantiate(CharacterBar);
It makes sense that CharacterBar = Instantiate(CharacterBar); doesn't have a parent so I looked it up and everyone apparently use:
GameObject g = (GameObject)Instantiate(gameObject, transform.position, Quaternion.identity);
g.transform.SetParent(transform.parent, false);
g.transform.position = transform.position;
But I don't understand this, I mean "g" is my CharacterBar so I get the following?
GameObject CharacterBar = (GameObject)Instantiate(gameObject, transform.position, Quaternion.identity);
CharacterBar.transform.SetParent(transform.CharacterInfoPanel, false);
CharacterBar.transform.position = transform.position;
This doesn't give me an error but it doesn't do anything either. I'm confused and I have no idea what path I should try
Something not quite right, in CreateCharacterInfoPanel, you pass the CharacterBar as prefab and store the new object in CharacterBar. I don't think you mean to do so.
"It makes sense that CharacterBar = Instantiate(CharacterBar); doesn't have a parent so I looked it up and everyone apparently use"
you say CharacterBar is an image, so it has to be parented to a Canvas or it won't be rendered.
Try simple to better understand:
public GameObject theImagePanel;
void Start ()
{
//find CharacterInfoPanel
GameObject mainCanvas= GameObject.Find("$$anonymous$$ainCanvas");
GameObject g = (GameObject)Instantiate(theImagePanel, transform.position, Quaternion.identity);
g.transform.SetParent(mainCanvas.transform, false);
// Not sure where to put it so you'll sort out the positioning I guess
}
Answer by hexagonius · Dec 29, 2018 at 02:56 PM
use the generic version of GetComponent:
https://docs.unity3d.com/Manual/GenericFunctions.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612184738im_/https://answers.unity.com/themes/thub/images/avi.jpg)