#Button OnClick() UnityEvent

1 messages · Page 1 of 1 (latest)

glad drum
#

Okay let's run through the checklist

  1. There are no errors in the console, either before or after you enter play mode.

  2. Your Button component is on a game object which is the child of an object with the Canvas component.

  3. There is a game object with an Event System component on it in your scene.

  4. Your script has the class declaration public class GachaStart : MonoBehavior {, and it's inside a file named "GachaStart.cs"

  5. You've dragged that "GachaStart.cs" file from the Assets directory in your Project window to some game object in your scene.

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

  7. You've selected "GachaStart > OnClick" from the "Function" drop down in the "On Click()" element on the Button component.

  8. 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 😅

fathom dragon
#
  1. 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

fathom dragon
naive pulsar
fathom dragon
#

2nd

naive pulsar
#

how do you make it?

#

in code I mean

fathom dragon
# naive pulsar in code I mean

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);
    }
}
fathom dragon
naive pulsar
fathom dragon
#

and the arrow appear as intended

naive pulsar
#

where does script "gachastart" hang?

#

we write scripts form capital btw

fathom dragon
fathom dragon
naive pulsar
naive pulsar
fathom dragon
#

my script is in the button

#

it would not work otherwise

naive pulsar
#

wait

#

I think we have problems with English🙄

#

show your gachastart gameObject @fathom dragon

#

and I would rather this process was a bit quicker

fathom dragon
naive pulsar
#

you make gachastart gameObject inactive

#

here: gameObject.SetActive(false);

fathom dragon
#

yes, theres the button, with the script in :

naive pulsar
#

stop, wait

#

why do you even have gachastart script on both button and "gachastart" gm

#

show me full inspector of that button

fathom dragon
naive pulsar
#

show me full unity with this inspector too

fathom dragon
naive pulsar
#

are you sure it is not?

#

so the button is definitely clicked?

fathom dragon
fathom dragon
naive pulsar
#

try writing the name of the gameObject when clicking

fathom dragon
#

if it was inactive it would be as the arrowimage

naive pulsar
#
print(name + " was clicked");
#

you have several script references

fathom dragon
fathom dragon
naive pulsar
naive pulsar
#

"gachastart" was clicked, not the button

#

do you understand now?

fathom dragon
naive pulsar
fathom dragon
# naive pulsar 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

naive pulsar
#

you cannot access Button component if GO does not have it

fathom dragon
#

go ?

naive pulsar
fathom dragon
naive pulsar
fathom dragon
naive pulsar
#

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 ?

fathom dragon
naive pulsar
fathom dragon
#

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?

naive pulsar
fathom dragon
#

@glad drum can u help me there ? i unfortunately struggle to understand what idbb say :/

glad drum
#

(or do you want the script to be able to do either, so you can use it in multiple ways/places?)

fathom dragon
glad drum
#

Sure you can! There's no reason it needs to be in both places :)

fathom dragon
#

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)

naive pulsar
fathom dragon
naive pulsar
#

hold on

glad drum
naive pulsar
#

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
{
    // ...
}
glad drum
#

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:

naive pulsar
naive pulsar
fathom dragon
fathom dragon
naive pulsar
glad drum
#

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.

naive pulsar
#

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

fathom dragon
#

i should put the button

#

and not the script

naive pulsar
#
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
            });
        });
    }
}
glad drum
#

The naming of all these things does not make this conversation any easier 😁

fathom dragon
#

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 ?

glad drum
#

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

fathom dragon
#

oh that worked

#

after 2 days

#

we finally made a button disapear

#

amazing

#

thank you so much mate XD

glad drum
#

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

fathom dragon
#

@naive pulsaryou to !

fathom dragon
#

by curiosity, how many time did it make that ur using unity ?

glad drum
fathom dragon
glad drum
#

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 👍

fathom dragon
glad drum
#

Keep at it 😁

naive pulsar
#

but I am glad you both have solved it

fathom dragon
naive pulsar
fathom dragon
#

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 ? ^^

glad drum
# fathom dragon <@1097950947369562187> 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;
        }
fathom dragon