I cant assign a function to a button. *free candy for whoever helps me out*
I cannot- for the life of me- assign a function to this button. i've tried everything. I've tried reloading unity, renaming the script, buttons, and empty game object that the button is assigned to. Nothing works. I'm trying to get it so that when i click the button, it loads another one of my scenes (and yes, I added it to my build already. I think my script should be fine but it isn't working. this is the script i'm using.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonManager : MonoBehaviour
{
public void NewGameButton(string newGameLevel)
{
SceneManager.LoadScene(newGameLevel);
}
}
After I attach it to an empty game object and then to the button,
and then i get the weird default stuff that i have no idea how to use (and doesnt work here anyways)
Please give me simple step by step instructions on how to fic this lol as im new to unity and scripting and im only 14
Answer by TSI25 · Oct 12, 2017 at 10:03 PM
I think you just want to click on this option
and then youll put in the name of the scene youre trying to load in the field that pops up.
as a side note you might also want to rename that function something like LoadScene so that its more recognizable.
Thank you so much! i gave you all the reputation points i could (1) lol but you really are a life saver. i also took your advice and renamed function. also i just wanted to ask- could i have your email? I know nobody who knows anything about c# or unity , and most of the online tutorials are made for people who are already experienced in c# and or unity.. You are a g o d s e n d.
@tarekhfahmi if an answer solves your problem you must accept it, and probably also upvote it. This automatically awards much more reputation points than you manually give.
Please read the FAQ: https://answers.unity.com/page/faq.html
If you are just posting questions and are not accepting/upvoting answers/comments, people will notice after a while and might stop helping you.
Answer by SirPotoo · Dec 14, 2018 at 07:09 PM
Or maybe create a new gameobject and put this on it:
using whatever blah blah;
public button BUTTON; //creates a popup in inspector which will allow you to pick any button
public class Whatever : MonoBehaviour {
void start(){
BUTTON.onClick.AddListener(MyScript);
}
void update(){
}
public void MyScript(){
// your script
}
}