Search Unity

Button On Click event not running (2019.3.6)

Discussion in 'Scripting' started by PinkButterfIy, Mar 25, 2021.

  1. PinkButterfIy

    PinkButterfIy

    Joined:
    Mar 24, 2020
    Posts:
    3
    I am working on a small game for college which relies on multiple buttons. I have the event systems and all components have all inputs filled as they should be but last time I worked on it they worked and now they don't. The button changes colour when pressed as it's supposed to but no code seems to run. One of the buttons I have does work and the only difference I can see is that the code used to run that one doesn't require any input data as it is used to load scenes.

    Capture.PNG
    My event system components
    Capture1.PNG
    the button component, these are basically the same for all of them, only the actual code that should be run changes
    Capture2.PNG
    Where the script should be being called from

    This is just one of many buttons but they're all set up in the exact same way. I did copy them across scenes because I have multiple scenes that use the same elements, I read that this can cause an issue but I tried remaking them and still no luck, and they have worked previously.

    If anyone can help I'd appreciate it, I was supposed to be loading it up to get evidence for my coursework but instead found a new issue that previously didn't exist so am on a tight schedule for getting it fixed.
    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    I don't think this is generally doable. You might get it to work if you duplicated the scene and then removed stuff from the duplicate, but I think just dragging it to a new scene breaks the linkages.

    At the end of the day you have to track down the GameObject and the script instance and the method that it is connected to, and when you move stuff from scene to scene (effectively different documents), the linkages may no longer be valid, because obviously they are in the other scene, and you cannot cross-link stuff between scenes.

    It's also possible that the event entry in that onClick list is not usable anymore because it still has invisible linkage to the previous scene: you might need to use (-) to remove it and (+) to make a fresh one, and then it should work.

    I'm sorry you've had this issue. Please consider using source control in order to guard and protect your hard-earned work.

    Personally I use git because it is free and there are tons of tutorials out there to help you set it up.

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837
     
  3. PinkButterfIy

    PinkButterfIy

    Joined:
    Mar 24, 2020
    Posts:
    3
    I used ctrl c, ctrl v to copy them, not directly dragging them but the scripts are accessible by all scenes in my project and it worked previously. I left it for a bit to focus on other things, came back and they didn't work
    I tried this too, sorry I should've said in my original post. Remade the component it is linked to as well but neither worked. Can't actually list all the things I've checked or tried but there's sure to be a few advised to me on this thread.

    Thanks for the recommendation, I've turned on the collab feature because I read earlier it allows for older versions to be restored as long as they've been published so hopefully I can get into the habit of doing so regularly
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you've remade everything and it still doesn't work, then your problem has nothing to do with copying; there's something wrong with the way you've set up the new scene.

    I'd start by making sure your button's onClick is pointing at the right object. If you click once on the object field (the part of your button screenshot that says "BOO") then it should highlight the corresponding object in your hierarchy. Make sure that's what you expected it to be. (Also note this is the part that will tend to break when copying to a new scene.)

    If that looks correct, next step would probably be to add a Debug.Log as the first line of the function being called in order to check that it's being run. If you see the log message, then the button is fine and the problem is in what the function does. If you don't see the log message, then probably the button isn't actually being clicked, and I'd start checking things like whether it's enabled and interactable, if there's any invisible objects in front of it, whether your Canvas has a Graphic Raycaster, etc.
     
    Putcho, PinkButterfIy and Kurt-Dekker like this.
  5. PinkButterfIy

    PinkButterfIy

    Joined:
    Mar 24, 2020
    Posts:
    3
    I can't believe it was that obvious. I had made a prefab of the "BOO" and when refilling all the variables, it sometimes clears them when I go back to it, I'd filled in the prefab which doesn't have all the fields filled because they're scene dependent instead of the instance of "BOO" in the scene. If that makes any sense. Thank you so much for that, spent hours yesterday trying to fix it
     
    Kurt-Dekker likes this.