#Button OnClick() UnityEvent
1 messages · Page 1 of 1 (latest)
Okay let's run through the checklist
-
There are no errors in the console, either before or after you enter play mode.
-
Your Button component is on a game object which is the child of an object with the Canvas component.
-
There is a game object with an Event System component on it in your scene.
-
Your script has the class declaration
public class GachaStart : MonoBehavior {, and it's inside a file named "GachaStart.cs" -
You've dragged that "GachaStart.cs" file from the Assets directory in your Project window to some game object in your scene.
-
You've dragged that game object which now has the GachaStart component attached to it to the "On Click()" object field in the Button's inspector.
-
You've selected "GachaStart > OnClick" from the "Function" drop down in the "On Click()" element on the Button component.
-
There's not some other UI element which extends in front of your button, and may be intercepting click events. If there is, then the button will not change visually when you click on it.
If all of the above is correct, it should be working 😅
- no error at all
3 there is no event component
and all the other are good x)
(sorry @glad drumi was asleep x) )
so i added the event system, now the button got clicked but i got a new error i didnt had before (wish means we are progressing xD)
" NullReferenceException: Object reference not set to an instance of an object" but if i understand it well, that means my reference to the button isnt correct, gotta check for typo
mhhh
as we can see in both screen, my button is well named...
ok so i fixed it, now the arrowimage appear, but the button stay there but it's supposed to disapear :/ xD
what's the problem?
the button is supposed to disapear when i click on the button, but it dont
by disappear you mean "deleting it" or "making is inactive"?
2nd
using UnityEngine;
using UnityEngine.UI;
public class gachastart : MonoBehaviour
{
public Image arrowImage;
public void OnClick()
{
Debug.Log("Bouton GachaButton cliqué !");
gameObject.SetActive(false);
arrowImage.gameObject.SetActive(true);
}
}
do you call it?
yep
is "Bouton GachaButton cliqué !" written in console?
yes
and the arrow appear as intended
there, and i didnt name it with the traditional form because it's in this thing
is it button?
no thats the object in wish i've putted the script in order to put it in the button
yes, how to you pretend to disable the button if you disable gameObject that the script hangs on?
i dont understand
what exactly?
wait
I think we have problems with English🙄
show your gachastart gameObject @fathom dragon
and I would rather this process was a bit quicker
gachastart is not the button
you make gachastart gameObject inactive
here: gameObject.SetActive(false);
yes, read my previous messages
stop, wait
why do you even have gachastart script on both button and "gachastart" gm
show me full inspector of that button
show me full unity with this inspector too
it must be set to inactive
are you sure it is not?
so the button is definitely clicked?
yes
yes
try writing the name of the gameObject when clicking
if it was inactive it would be as the arrowimage
so how can i fix this ?
sorry, I just needed to go for a while
as I have expected
"gachastart" was clicked, not the button
do you understand now?
yeah, but how do i fix it
you need to make the button be clicked, not gachastart
so i changed the script to be that
using UnityEngine;
using UnityEngine.UI;
public class gachastart : MonoBehaviour
{
public Image arrowImage;
private Button GachaStartButton;
private void Start()
{
GachaStartButton = GetComponent<Button>();
}
public void OnClick()
{
Debug.Log("Bouton GachaButton cliqué !");
GachaStartButton.gameObject.SetActive(false);
arrowImage.gameObject.SetActive(true);
}
}
but no i have the error
NullReferenceException: Object reference not set to an instance of an object
this error appears here:
GachaStartButton = GetComponent<Button>();
you cannot access Button component if GO does not have it
go ?
GameObject
how do i declare it ?
sorry?
how do i give it to GO
give what? button?
you do not need to
you don't have button on "gachastart" GO and you are trying to access it
it should be quite logical what to do with it I guess ?
then how do i create one
maybe you could ask so that I can understand?
🤦♀️
u told me i dont have my button in the GO, so i want to know how do i fix it, do i create a new button, do i create a new reference into the GO, what im i supposed to do?
there is a huge amount of possibilities according to what you need:
-
delete the script from Game Objects that do not have Button component
-
add button components to all Game Objects that should have the script
-
check whether GO has Button component when accessing it
how do i do step 3
@glad drum can u help me there ? i unfortunately struggle to understand what idbb say :/
Do you want your "gachastart" script to live on the Game Object with the Button, or do you want it to live separately from the button, on the "gachastart" Game Object?
(or do you want the script to be able to do either, so you can use it in multiple ways/places?)
well actually, if i dont put it on an empty object on the scene i cant put it in the button :/
Sure you can! There's no reason it needs to be in both places :)
i must have done it the wrong way then, cause when i try to take it from the assets folder and drop it in the script placeholder of the button, it just show an sign saying i cant x)
I guess I have written everything nicely
yeah but unfortunatly your probably not english, neither do i, so comunication is hard x)
no, I think that's because of your c# 🙄 and I am not trying to hurt you, sorry if I did
hold on
Ah that's a good thing. You wouldn't want to do that... If you drag the script from the Assets folder to the OnClick() inspector, then it wouldn't refer to a component in your scene... You would be trying to run code which is not in your scene, so it would not do anything.
that's not step 3, that's 3rd possibility
you can check whether GachaStartButton is null when using it
anyway I do not think it's a good idea
you can also
[RequireComponent(typeof(UnityEngine.UI.Button))] // I hope correct type
public class gachastart : MonoBehaviour
{
// ...
}
But like IDBB was saying, this line:
GachaStartButton = GetComponent<Button>();
It tries to get a reference to a Button component which is attached to the same game object which the script is attached to.
So when you put this script on an empty Game Object - your "gachastart" Game Object - that Game Object does not have a Button component on it. So GetComponent<Button>() returns null, and so GachaStartButton is null.
But you also have this script attached to the "GachaStartButton" Game Object. Because "GachaStartButton" does have a Button component attached to it, GetComponent<Button>() does return a reference to the button in that case:
yes, that's evactly what I was trying to say
but this line is enough I guess 🤔
okay, im following, thats effectively what idbb was saying, so how can i fix this 🤔
it was to syntethised, i didnt understand
I have literally gave you 3 possibilities 💀💀
So to make the script on the "GachaStartButton" Game Object work, you would drag the "GachaStartButton" Game Object into the OnClick() inspector - you want it to access the script component which is already on "GachaStartButton". If you did this, then you could delete the "gachastart" Game Object - there's no reason to have two copies of the script for this one button.
you should first understand whether you really need to have gachastart script on gachastart GO
you could also consider redacting your script for multiple buttons if you need it
sososo, in the onclick()
i should put the button
and not the script
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class gachastart : MonoBehaviour
{
[SerializeField] private Button[] buttons;
private void Start()
{
buttons.ToList().ForEach(button =>
{
button.onClick.AddListener(_ =>
{
// do smth
});
});
}
}
Put the "GachaStartButton" Game Object in the field, so you can access the gachastart script which is attached to it.
The naming of all these things does not make this conversation any easier 😁
im really sorry x)
so i put the GachaStartButton, in the onclick() of the inspector, i still have the same error
do i have to remove the gachastart for it to work ?
The error is from the script running on the "gachastart" Game Object, yes - because it is still trying to get the Button component attached to "gachastart," but "gachastart" still does not have a button. You can delete "gachastart," yes
oh that worked
after 2 days
we finally made a button disapear
amazing
thank you so much mate XD
The other way you could have done this - if you wanted to make it work with the "gachastart" GameObject instead - you would change your code so that you can manually set GachaStartButton instead of using GetComponent<Button>() - because in this case the script would not be on the Game Object which has the Button component.
using UnityEngine;
using UnityEngine.UI;
public class gachastart : MonoBehaviour
{
public Image arrowImage;
public Button GachaStartButton;
public void OnClick()
{
Debug.Log("Bouton GachaButton cliqué !");
GachaStartButton.gameObject.SetActive(false);
arrowImage.gameObject.SetActive(true);
}
}
In this case you would drag the "GachaStartButton" Game Object into the "Gacha Start Button" field in the "gachastart" Game Object's inspector, so public Button GachaStartButton would now refer to the Button component on the "GachaStartButton" Game Object. You would drag the "gachastart" Game Object in to the OnClick() inspector. And you would remove the gachastart script component from the "GachaStartButton" Game Object
@naive pulsaryou to !
i thaught about it ! and i tried it, but i think, now that im seeing it written well, that i didnt understand it would have been on gachastart so, seeing nothing appear in the GachaStartButton i thaught it didnt work xd
by curiosity, how many time did it make that ur using unity ?
Like, how long have I been using Unity?
yes ^^
I started learning it... three months ago, I think?
I've been coding for many years, but I'm new to C# as well.
The Junior Programmer Pathway on Unity Learn was really helpful for figuring out the Unity basics 👍
amazing! i hope i'll be that good in three month !
Keep at it 😁
I am sorry, I was just missing the full conversation because of some reasons
but I am glad you both have solved it
oh dont worry! u were really helpfull ! thank a lot
write here if something if another issue happens, I have notifications switched
yo guys i have a new problem !
this is the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CardScrollAnimation : MonoBehaviour
{
[SerializeField] private List<Color> rarityColors = new List<Color>();
void Start()
{
for (int i = 0; i < 10; i++)
{
GameObject card = new GameObject("Card" + i);
card.transform.SetParent(transform);
RectTransform rectTransform = card.AddComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(150, 200);
Image image = card.AddComponent<Image>();
image.color = rarityColors[Random.Range(0, rarityColors.Count)];
Vector3 position = new Vector3(0, 70, 0);
card.transform.localPosition = position;
}
}
void Update()
{
float scrollSpeed = 100.0f;
transform.Translate(Vector2.left * scrollSpeed * Time.deltaTime);
}
}
basically, everything work, but the image dont appear :/
@glad drum can u try to help me ? ^^
Does that throw any errors?
I think
RectTransform rectTransform = card.AddComponent<RectTransform>();
Might work a little strangely... I don't think a GameObject can have both a normal Transform and a RectTransform (the transform property either refers to the GO's Transform or it's RectTransform)... I'm not sure what Unity would do in that case. You might try creating the GameObject along with RectTransform to start with, just to rule that out as a problem.
for (int i = 0; i < 10; i++)
{
GameObject card = new GameObject("Card" + i, typeof(RectTransform));
card.transform.SetParent(transform);
RectTransform rectTransform = card.transform as RectTransform;
rectTransform.sizeDelta = new Vector2(150, 200);
Image image = card.AddComponent<Image>();
image.color = rarityColors[Random.Range(0, rarityColors.Count)];
Vector3 position = new Vector3(0, 70, 0);
card.transform.localPosition = position;
}
Nop x) in the end everything worked i solved it x)