[SOLVED] Instantiate for menu system, So confused. Have read the guide still confused.

Hi there,

I’ve been trying for days to do something simple.
I’m making a menu system

here is code:

using UnityEngine;
using System.Collections;

public class MenuManager : MonoBehaviour
{
public int MenuSel = 0; //0 = New Game, 1= Options, 2= Credits, 3=Exit
public GameObject NewGameLit; //Sprite Prefab highlight newgame lit
public GameObject OptionsLit;
public GameObject CreditsLit;
public GameObject ExitLit;
public GameObject NewGameUnlit;
public GameObject OptionsUnlit;
public GameObject CreditsUnlit;
public GameObject ExitUnlit;
public Transform parentobject;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

if (Input.GetKeyDown(KeyCode.Space))
LaunchOption();

if (Input.GetKey(KeyCode.UpArrow))
{
MenuSel = MenuSel - 1;
if (MenuSel == -1) MenuSel = 4;
Debug.Log(“UP”);
}

if (Input.GetKey(KeyCode.DownArrow))
{
MenuSel = MenuSel + 1;
if (MenuSel == 5) MenuSel = 0;
Debug.Log(“DOWN”);
}
SpriteUpdate(); // SPRITE UPDATE

{
if (MenuSel == 0) //Highlight New Game
{
NewGameLit.transform.parent = parentobject;
Instantiate(NewGameLit.transform.parent.);

}

}

}

void SpriteUpdate() //Update Sprites
{

Debug.Log(“Sprite Update”);
Debug.Log(MenuSel);
//make sure its deltatime
//Place All Unlits on canvas

if (MenuSel == 0) { } //New Game SpriteLight

//return;

}

void LaunchOption() //Execute the Selected Option
{
Debug.Log(“SELECTED”);
}

}

The bolded area. when i run the script the new menu items dont appear where i want it. (like they are not part of the parent object) When i manually drag the prefab into the menusystem it lines up correctly. When i instantiant its doesnt work. It seems im not getting this right. anyone any ideas?

You can see from the pic prior post where the “new Game” object shows upon the bottom half cut off.
I have attached a pic of the menu system how i want it to look by placing the prefabs in the game object manually


So Nobody huh!!

You can see the error in the console in your screenshot. You’re setting the parent on the prefab, then instantiating. You need to set the parent on the instantiated object, not the prefab.

Don’t ignore console errors.

2 Likes

OK, let me try fix that. Thanks.
https://forum.unity3d.com/threads/setting-the-parent-of-a-transform-which-resides-in-a-prefab.30496/
http://answers.unity3d.com/questions/341714/setting-the-parent-of-a-transform-which-resides-in.html

OK

GameObject newgame = Instantiate(NewGameLit);
newgame.transform.parent = parentobject;

It works again without error. Thank for the nudge i will pay more close attention to console errors.
I still doesn’t appear where i want it, but its a step forward so much appreciated. Apparently i should be using the GUI for this stuff, where global positions are used… Brain hurt now.

Might check out this tutorial and try this:
https://www.youtube.com/watch?v=pT4uca2bSgc

You can use the (awfully) named ScreenPointToLocalPointInRectangle in the RectTransformUtility to calculate the correct position.

However – if all you’re trying to do is change a sprite’s graphic during rollover or click, use the Button UI component and change the transition mode from Color Tint to Sprite Swap and set your images as appropriately in the inspector.

Oh man your awesome, i will try that tonight

Yes that works Marvelously, you must have been laughing at how difficult i tried to make that. lol

I typically use layout elements to define the positions for me, rather then defining positions via code.

2 Likes

Thanks Guys, well i uploaded it to www.trumpaustralia.net.au. Got a basic menu happening with your help guys. Feeling more confident in unity now. However, I have got it working on PC Build, but will not work on WEBGL. I’m getting this weird error. as attached.

OK, answered my own question. Now it apears u need to use SceneManager.LoadScene(“Scene)” and for this to work you should i suppose include “using UnityEngine.SceneManagement;” at the top of the code or you can write out longer lines.
http://answers.unity3d.com/answers/1284911/view.html

Nah, im still getting it. Damn. I’m off to bed. Try figure this out tomorrow