Unity Button onClick() only showing default functions - c#

I'm trying to execute a function using a Button in Unity, but the dropdown menu for the funtions only shows me the default functions that all GameObjects have
I have created an empty GameObject that has the HighscoreList script and draged this object onto the onClick() Object selector, but I can't find any of the functions that I created
What the editor shows me:
The function im trying to call:
public void AddItem(int score, string name)
{
Node n = new Node(new HighscoreData(score, name));
start = start.addItem(n);
}

Through unity event you can only have functions with a maximum of 1 parameter.

If the script does not compile errors and inherits MonoBehavior.
This reason may be caused by directly mounting the script to the OnClick event. However, the OnClick event should use the GameObject containing the script.
Here are relevant steps:
Mount the script on the parent object.
GameObject should be mounted on the OnClick event.
The results are as follows.

Related

Unity - Assign Button in Scene to Prefab

I'm having trouble assigning two Buttons to the Prefab after it is instantiated.
The buttons are in the scene and i don't know how to assign them.
Drag and Drop doesn't work of course. I'm aware of that.
When I make something like
btnnext = GameObject.Find("Next").GetComponent<Button>();
in the Start() function of the Prefabs script it doesn't work either.
Are there any other workarounds?
As #slowikowskiarkadiusz said GameObject.Find is not the best solution, because it's slow and error prone. But there is an easy solution.
On the script which is placed on the prefab make a public function, called AssignButton:
class ScriptOnPrefab : MonoBehaviour {
public void AssignButton(Button button) {
btnnext = button;
}
}
In the script where you instantite the prefab, you then link the buttons and assign them:
var instance = Instantiate(prefab);
var scriptOnPrefab = instance.GetComponent<ScriptOnPrefab>();
scriptOnPrefab.AssignButton(button);
Note: For this to work that the ScriptOnPrefab has to be on the root of the prefab, note on child objects.
Note: prefab is linked as a GameObject.
Note: If you link the prefab as ScriptOnPrefab you can skip the GetComponent step and immediatelly call the Assign method.
By 'doesn't work either' you mean that btnnext is null or you mean that .GetComponent<Button>() throws an exception? The first case would mean the object named "Next" that was found doesn't have a Button component on it. You can check if you're expecting it to have the Button component or maybe something slightly different. You could also have multiple objects named "Next" and the one you're getting is not the one you expect to get. The second case would mean that your object is most likely inactive. Find(string) doesn't search within inactive objects.
That being said - Find(string) is not reliable in any capacity and I would advice to avoid it (it's also terribly slow). Instead I would create a script to be placed on the object with the button. Inside of that script in the Awake() method I would assign the instance of the Button component to some kind of a public static field, so the other script can later pick it up (if you are dealing with two buttons it might be a list or two separate fields. Depends on your case I guess).

Unable to Change OnClick function parameter in Inspector

I'm still learning Unity and just trying to create simple games to get the hang of things. I ran into an issue where I can't edit the parameter of my method when its assigned in the OnClick method in the inspector in a button. I attached my script to a game object and place the object into the OnClick function and select the correct function but I cannot edit the parameter field...
Is it supposed to behave like this or am I missing something? The function is public and I'm not sure what else I should be looking at. I'm trying to assign the values to the buttons in the grid so that when they click a space (button) in the TicTacToe grid it sends the value to the controller so that it knows which space will have to display the player's sprite.
public void TicTacToe(int space)
Any help is appreciated.

GetComponent function returning null

I'm developing a simple game in Unity 2017 in C#.
In my levels menu I have a Text object with a button component in it, and a script attached to it.
This is what's currently in the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelLock : MonoBehaviour {
public bool isLocked = true;
private Button lvlBtn;
// Use this for initialization
void Start () {
lvlBtn = GetComponent<Button> ();
if (lvlBtn != null) {
Debug.Log ("this should be working");
} else {
Debug.Log ("you did something wrong");
}
//if(isLocked){
// lvlBtn.enabled = false;
//}
}
}
The problem is, that GetComponent<Button>() is returning null, meaning it's not finding the button component in the object.
I tried adding an Animator component and perform the same operation just to check and it did work, so I have no clue why it shouldn't find the button component. If anyone could help with this or point out to me something i'm doing wrong, I would really appreciate it
EDIT:
Inspector tab:
Console tab:
Your screenshot shows that you attached the LevelLock script to the Button which is fine.
Possible reasons and fixes:
1.The component/script is not even attached to the GameObject or is attached to the wrong Object.
In, your code, calling GetComponent<Button>(); without finding the GameObject first means that GetComponent should look for a Button component that is attached to this GameObject this script(LevelLock) is attached to.
If the script you want to retrieve, is attached to another GameObject, you can solve this by first, finding the GameObject the Button component is attached to with GameObject.Find then retrieving the Button component/script from it.
Replace
lvlBtn = GetComponent<Button> ();
with
GameObject obj = GameObject.Find("lvl 2");
lvlBtn = obj.GetComponent<Button>();
3.Script is attached to multiple Objects
One of the reasons GetComponent<Button>() might return false is because you mistakenly attached the LevelLock script to another GameObject. It could be an empty GameObject but that GameObject doesn't have the Button component attached to it.
Find this GameObject and remove the LevelLock script from it.
To do that, select the LevelLock script from the Project tab then Go to Assets ---> Find References In Scene. Remove the LevelLock script from other GameObjects.
This will make sure that the LevelLock script is only attached to the "lvl 2" GameObject
3.Multiple scripts with the-same name but in different namespace:
If you have two classes with the-same name but in different namespace and you attached one to the GameObject while trying to access the other one from code, it will be null since they are not the-same. I looked into your project and this was the Problem.
You created a script named "Button" while Unity comes with it's own built-in script/component also named "Button" which is in the UnityEngine.UI namespace.
You attached the Unity's built-in "Button" script to your GameObject but then you are trying to access your own made "Button" script from the script. Supply the namespace which is UnityEngine.UI so that Unity will know that you want to use the Button component that comes with Unity.
Change:
vlBtn = GetComponent<Button> ();
to
lvlBtn = GetComponent<UnityEngine.UI.Button>();
Also change:
private Button lvlBtn;
to
private UnityEngine.UI.Button lvlBtn;
Finally, do not name your script the-same name as any built-in component like Button, Image, Text, Rigidbody and so on. Create a class name and make sure that it doesn't exist with Unity API otherwise you'll continue to run into such issues and even people on SO won't be able to help you unless they have access to your project.

Button behaviour on Unity

I am creating a small application which is instantiated by a client. In my application I have 2 classes MessageQueueSingleton.cs and DataObject.cs
MessageQueueSingleton is a singleton class which contains a static Dictionary of the objects of DataObject class. MessageQueueSingleton has some methods which will manipulate the attributes of the DataObject class according to the instructions from the client.
My problem is I also need a Unity GUI which can call few methods of MessageQueueSingleton, i.e. if I press a button on UI, a method will be called from the MessageQueueSingleton.
I am new to unity programming and I tried to look the examples which shows how to connect scripts to buttons, but I still can't see my method in the "Inspector" onClick() section. I am using Unity 5.5.1f1
Any small example or link describing the process to do it would be helpful.
Thanks.
On your GameObject with a Button component you can add a script component with the following code to set up a listener and method for that button:
void Start() {
var button = GameObject.Find("GameObjectWithMyButton").GetComponent<Button>();
button.onClick.AddListener(MyMethod);
}
void MyMethod() {
// Do something when button is clicked.
}
And if you want to drag & select it in the inspector, make sure the methods you want to select in the inspector button onclick GUI thing are public methods! To make the public methods of a script shows up in the Inspector Button Onclick GUI you place the script on a GameObject then drag that object to the button onclick GUI and select them from the dropdown. They won't show unless they're public methods. (public void DoSomething() { })

Load scene using canvas button

void OnMouseDown() {
SceneManager.LoadScene ("Scene2");
}
I have tried every conceivable method. The method posted has worked for me using GameObjects with colliders. Instead, this time I am using a button on a 2D canvas. It does not work in this context.
How do I load a new scene using a button in a canvas? I have tried so many different things. This should be simple.
Thanks for any advice.
Here (link: Unity page) you can find a video tutorial how to use Button on canvas in UnityGUI. It's for Unity 4.6 but its really simillar to newest (5.3.1).
It's quite simple. U can make a script with public method e.g
public void LoadScene2()
{
SceneManager.LoadScene ("Scene2");
}
Attach this script to some GameObject e.g Controller. And add event in Button inspector.
In my opinion there is a better solution for the one shown by #Paweł Marecki
I use this in my projects.
OK so you will simply create a script called ButtonManager and inside it you can make a method like this
public void ChangeToScene(string sceneName)
{
Application.LoadLevel(sceneName);
OR
SceneManager.LoadScene(sceneName);
}
Now you have your canvas button, you will select it and look for "Event Trigger"
(i got this image from google to help) add a new mouse down event.
Create an empty GameObject on your Scene, name it "ButtonManager" and drag it onto the event box.
Now you need to click that dropDown list and find your "ChangeToScene" method.
You will see that an editor field appears below, type your desired scene name and hit play :P
This way you will always use this script when you want to change scenes.
You can add other methods and add functionality, but the beautiful part is that you dont need to create a method each time the name of the scene changes.

Categories