Hey guys, so this is my first time posting in the Unity Forums and I’ve only been working with C# seriously for about a year so I’m still learning.
The main problem I’m having with my script is that I’m trying to replace the gameObject that this script is attached to with a prefab from my assets. That part doesn’t seem to be the issue, as the prefab will instantiate into the scene hierarchy and the previous object disappears, but because I’m using a canvas to render my images, the prefab won’t render due to it not spawning as a child of the canvas.
My prefab and all of its child objects have already been set using RectTransforms and Image/Canvas Renderers, but I can’t seem to adjust their parent upon instantiation. I’ve uploaded the script I’m working with, so any help at all would be appreciated. I keep scouring the forums but I can’t seem to find anything that works.
Thanks guys, and if you have any questions about my issue or need me to clear anything up please let me know!
I’ve tried this before, but every time it tries to compile I get these two errors in the Unity console for line 14 where I try to declare the canvas as the parent of the object:
error CS1502: The best overloaded match for UnityEngine.Transform.SetParent(UnityEngine.Transform) has some invalid arguments
error CS1503: Argument ‘#1’ cannot convert ‘UnityEngine.GameObject’ expression type to ‘UnityEngine.Transform’
You’re simply indicating the wrong parent. Probably, you are passing it the parent gameobject, while the parent gameobject’s transform component is expected.
You guys were right! I originally didn’t attach a transform to my new parent object within the parentheses. It’s working just fine now! Thanks so much.
The answer to the specific issue was given, just above your post.
If you’re having trouble, please post a thread of your own, with your script and question with an appropriate title and description in either the scripting or UI forum.
It was a couple of months ago, and that user hasn’t been back. However, the thread discusses the issue and explains solutions, already – including a line of code.