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 (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
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.
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.
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.
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