This is actually something I tried to before I got to this lecture, but I decided to wait for a good time to ask about this, and that time is now!
So I thought it would be cool if the “Play Again” and “Menu” buttons appeared in the same scene when the player ship is destroyed. I made two different text gameobjects, made sure they take you to the correct scene, made them prefabs, and instantiated them in the player script after health hits 0, using:
Instantiate (restart, gameObject.transform.position, Quaternion.identity);
Instantiate (menu, gameObject.transform.position, Quaternion.identity);
Then I wired the prefabs up in the inspector. So, so far so good! But when I play it and get destroyed, the buttons don’t appear in the scene, but they do in the hierarchy.
Of course, the problem is they’re not appearing under the Canvas. So I figure I need to do some adoption, and make the Restart and Menu buttons children of the Canvas in the script. After looking around online how to do this I decided to try the SetParent method, and going by the Unity docs I tried this:
public void Adoption (Canvas newParent) {
restart.transform.SetParent (Canvas);
}
And this gives me a CS1503 error message saying:
“cannot convert UnityEngine.GameObject' expression to type
UnityEngine.Transform’”.
Also when I hover over SetParent in the script it says “Unknown resolve error”. So here I am! Would love any help on this. Am I going about this completely wrong or have just got some of the code a bit wrong? Of course I realize I need to sort the positions of the buttons as well, but first I just need to get them to appear! Here’s my entire “PlayController” script for better context: