Main Menu/Start Button Scripting

I feel like i am missing something with my coding;

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadSceneOnClick : MonoBehaviour {

    // Use this for initialization
    public void LoadByIndex(int sceneIndex)
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Even after I drag and drop the coding to the On Click section of my start button, and change it to run time, the button that says No Function gives no LoadIndex option like the tutorials I have scene. Is it something wrong with my coding or is the problem elsewhere?

Well that was odd, just reattached it and fixed that problem, however when i click the start button it still doesn’t load the indexed scene 1. Anything I could be missing or forgot in the code above?
It doesn’t register that I am hovering over the button either…

  1. Make sure you add the scene to the build settings
  2. Your code looks…odd. You take a parameter called sceneIndex which is fine but then you use LoadScene with the index of the currently loaded scene completely nullifying the point of being able to supply your own scene index.
    Change:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

to

SceneManager.LoadScene(sceneIndex);

Then make sure to supply a build index on the On Click in the inspector

Apart from that, I see no obvious code-breaking issues.

Also, USE CODE TAGS!

Odd, I swear thats what i originally had in it and it wouldn’t work but I will do as you say and see if I was just messing up and sorry, I forgot how to do them until last night.
Hmm…even after changing the setting to make the button blue when the mouse hovers over it, it doesn’t change, so I don’t seem to actually be interacting with the button at all. Any clue why this could be?

Actually i am going to rebuild this part completely.

Why my script doesn’t work ? :confused: |
|
\ /
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayMenu : MonoBehaviour
{
public void PlayGame ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex = 1);
}

Start a fresh post please, as per forum rules. It’s FREE!

When you do, please keep this in mind:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand errors in general:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

change the number 0 to 1 and the code will work