- Home /
Using listeners to complete code errors
I am quite new and i do not yet understand event systems so when i wanted to make a scene load when i clicked a ui button so i did this - http://answers.unity3d.com/questions/836635/can-ui-buttons-load-scenes.html (first answer) then when i have been trying to swap arrow keys out for two ui button i used code in the exact same style and way as the code to load different scenes but run into problems.
Here is my scene loading code -
using UnityEngine;
using System.Collections;
public class ButtonNextLevel : MonoBehaviour
{
public void NextLevelButton(int index)
{
Application.LoadLevel(index);
}
public void NextLevelButton(string levelName)
{
Application.LoadLevel(levelName);
}
}
Here is the answer i saw it from and tried to copy the style -
Lets say it's 4.6 and the new UI.
I create a Canvas.(Add a canvas to the Hierarchy in the inspector for the scene) I Add a UI->Button to the canvas.
I create a script(ButtonNextLevel.cs), i figure i want to support level change by level name(string) and by level int(the index number in build settings).
using UnityEngine;
using System.Collections;
public class ButtonNextLevel : MonoBehaviour
{
public void NextLevelButton(int index)
{
Application.LoadLevel(index);
}
public void NextLevelButton(string levelName)
{
Application.LoadLevel(levelName);
}
}
I save this.
I add an empty game object. I attach my new script.
I associate this game object with my new script to the Button(in the inspector) by: Clicking the +(plus) button to add a callback from an object->script->method.
Furthermore i drag and drop that game object with the new script to the object box under On Click (). I select either ButtonNextLevel.NextLevel(int) or ButtonNextLevel.NextLevel(string)
I specify either the level name or index based off the method i selected.
I am good to go.
With this code -
using UnityEngine; using System.Collections;
public class Left : MonoBehaviour { public bool dodo; public new Vector3 position; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update () {
if (dodo == true)
{
GameObject player = GameObject.Find("Player");
Movement playerScript = player.GetComponent<Movement>();
position = playerScript.position;
if (position == new Vector3(0,1,-40))
{
playerScript.transform.position = new Vector3(-10,1,-40);
}
if (position == new Vector3(10,1,-40))
{
playerScript.transform.position = new Vector3(0,1,-40);
}
}
}
public void left (bool doOrDoNot)
{
dodo = (bool);
}
}
Either i am close to achieving my goal, in which case could you explain to me how i can achieve that goal. Otherwise i have bad understanding of UI in which case could you direct me to somewhere where i can understand it in clear precise detail suitable for a absolute beginner as i find the tutorial videos confusing and complex. Thanks