Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Script functions not showing up

Discussion in 'Scripting' started by willypd, Jun 29, 2023.

  1. willypd

    willypd

    Joined:
    Jun 29, 2023
    Posts:
    5
    Hello!

    I am trying to create a main menu as seen below:

    upload_2023-6-29_14-57-7.png

    The script entered for the menu is as follows:

    upload_2023-6-29_14-58-2.png

    The script is applied to the game object:

    upload_2023-6-29_14-58-59.png

    But when I go to assign the buttons the play and quite functions are missing?

    upload_2023-6-29_15-2-40.png

    Did I miss a step? Or is my script incorrect?

    Thanks in advance for any assistance with this!

    Sincerely,
    Brian upload_2023-6-29_14-57-7.png upload_2023-6-29_14-58-2.png upload_2023-6-29_14-58-59.png upload_2023-6-29_15-2-40.png upload_2023-6-29_14-57-7.png upload_2023-6-29_14-58-2.png upload_2023-6-29_14-58-59.png upload_2023-6-29_15-2-40.png upload_2023-6-29_14-57-7.png upload_2023-6-29_14-58-2.png upload_2023-6-29_14-58-59.png upload_2023-6-29_15-2-40.png
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,111
    Hi, welcome to the Unity forum :)

    First thing I would have asked was if you are 100% sure you have saved and loaded your scripts (try pressing CTRL +R in Unity) BUT then I realised your class name is Buttons
    public class Buttons : MonoBehaviour
    instead of MainMenu. Change "Buttons" to "MainMenu" and it should start to show up.

    Also (for future questions) please use code tags. People here do not react well to screenshots of code. It's the worst (sorry). We can not copy that code easily. The likelyhood of getting an aswers is directly related to HOW you ask the question, read this ;-)
     
    Yoreki likes this.
  3. willypd

    willypd

    Joined:
    Jun 29, 2023
    Posts:
    5
    Hello! Okay thank you I really appreciate the response and suggestions. You'll have to forgive me as I am basically brand new to all of this. Thanks in advance for your patience as I attempt to follow all suggestions.

    I am not sure if I input the code below correctly. But I have updated the code to MainMenu instead of Buttons. For the code tags that you mentioned if I did not do this correctly is this something that should be done in VS Code or in the widget here in the forum?

    As for the CTRL+R I did do that but didn't see any change or anything. After changing the code and saving I do now see this new message:

    Assets\Scripts\MainMenu.cs(6,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'MainMenu'

    ---------------------------------------------------------------------------------------

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class MainMenu : MonoBehaviour
    7. {
    8.   //Load Scene
    9.   public void Play()
    10.     {
    11.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    12.     }
    13.  
    14.     //Quit Game
    15.     public void Quit()
    16.     {
    17.         Application.Quit();
    18.         Debug.Log("Player Said I quit");
    19.     }
    20. }
    --------------------------------------------------------------------------------------------
     
  4. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,111
    Thanks for using the code tags :)

    Well this means you already have a script file with a MainMenu class in it. There can only be one (in the same namespace). Find it and either delete it or add the Play() and Quit() to it.
     
    Last edited: Jun 29, 2023
  5. willypd

    willypd

    Joined:
    Jun 29, 2023
    Posts:
    5
    Hmm I may need a little more clarification on this. Unfortunately, I'm not even sure how to ask the next question properly. I'll give it a try.

    I do only have one script file in my Assets > Scripts folder called MainMenu. That file has the above code included.

    When you say there can only be one MainMenu "class" in the same namespace where could these exist in order to delete them?
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    If you have another file with
    public class MainMenu
    in it, even if the name of the file isn't MainMenu.cs, it may cause that error.

    Unity needs the class names to match the filenames, so the system can find the script easily. The C# language itself does not care about that. The C# language does require there to be only one thing in a namespace called by a given name, and gives this error if it finds multiples.
     
  7. willypd

    willypd

    Joined:
    Jun 29, 2023
    Posts:
    5
    Okay bear with me here. When you say "another file" are you referring to script files only? I only have one script file in my project so there is not any other script files that could contain public class MainMenu. If other types of files could be interfering and causing that error what types would I be looking for?
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    Does that one script file only have one class in it?
     
  9. willypd

    willypd

    Joined:
    Jun 29, 2023
    Posts:
    5
    It does but I just found a duplicate of the MainMenu script file sitting in the root level of the assets folder! After deleting that I am now seeing the Play() and Quit() options in the dropdown menu as expected! Not sure how that dup ended up there but that seems to have set me straight for now! Thank you!! :)
     
    halley, mopthrow and _geo__ like this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563