0
\$\begingroup\$

I have two scenes the first one and the one that is loading when running the game is main menu. The second scene is scene002.

I want to load scene002.

In the main menu scene I have a button. Attached to the button this script:

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

public class LoadSceneOnClick : MonoBehaviour
{
    private bool loaded = false;

    public void LoadByIndex(int sceneIndex)
    {
        SceneManager.LoadScene(sceneIndex);

        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
    }

    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        if (arg0.isLoaded == true)
        {
            loaded = true;
        }
    }

    public void ActivatePlayer(GameObject player)
    {
        if (loaded == true)
        {
            player.SetActive(true);
        }
    }
}

When loading the scene002 i want to enable the Player.

In the button On Click I added to the list the two methods LoadByIndex and ActivatePlayer but when I click the button the player object I dragged to the inspector of the button is gone and I'm getting this exception:

ArgumentException: failed to convert parameters
System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:484)
System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:528)
System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:77)
UnityEngine.Events.PersistentCall.GetObjectCall (UnityEngine.Object target, System.Reflection.MethodInfo method, UnityEngine.Events.ArgumentCache arguments) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:469)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:436)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:609)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:775)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:811)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
\$\endgroup\$
3
  • \$\begingroup\$ Did you add scenes to build window? \$\endgroup\$ Commented May 12, 2018 at 10:21
  • \$\begingroup\$ @CandidMoon Yes first added to the file > Build Settings... the Main Menu scene then the second scene the one I want to load at index 1. \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 11:04
  • \$\begingroup\$ @CandidMoon Is it logic the way I try to do it ti activate the player ? I mean in the script isi t logic to do it this way ? \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 11:06

1 Answer 1

1
\$\begingroup\$

I suspect that scene loads successfully but your callback is giving you an error. Scene arg0, LoadSceneMode arg1 - might be wrong types.

But documentation says they are ok. https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

So it is possible that you are passing wrong type in the inspector. Considering that it makes a call to Reflection it's probably related to serialization in inspector. Check out button event and correct the value if it's wrong.

EDIT

From the discussion with OP I understood that the problem lies in additive scene loading.

  1. The problem was that the new scene was first loaded, losing all the data about previous scenes and then the button event tried to access that data.

  2. A theory that references between 2 additive loaded scenes don't work in the inspector. (Will update the answer once I test it)

\$\endgroup\$
11
  • \$\begingroup\$ What I'm passing as Player in the Inspector of the button is an object from the Hierarchy object name Player. The problem is once I'm running the game the Player I dragged in the Inspector is gone removed from the inspector. Maybe the problem is that the button is in one scene but the Player object is in another scene ? \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 12:13
  • \$\begingroup\$ My main problem is that in the main menu scene when I click the button it should load the second script. But in the second script I have Player a first person with a Camera. And if the Player will be set to true on then it will show me the player camera and I will be in the second scene when running the game. But I need to be in the main menu scene when running the game and then I need to set the player to true when loading scene: look at it here: gamedev.stackexchange.com/questions/158533/… \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 12:15
  • \$\begingroup\$ The problem is that in the scene i want to load i have a sky box and space station and more stuff i need to show with the main menu. So the active scene is the second one when running the game and i turned off the player so its camera won't be on and the main menu camera will be the main. That way i see the main menu with the second scene stuff. \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 12:18
  • \$\begingroup\$ Not sure how to explain it good enough. But when the Player camera is on it will be the main camera. That is why the player camera is off when running the game. \$\endgroup\$
    – Daniel Lip
    Commented May 12, 2018 at 12:20
  • \$\begingroup\$ @BenziAvrumi You are missing the reference to object in that button. Could you describe what you see in the button component event when you get this error after scene load? Is the reference to the player still there? \$\endgroup\$ Commented May 12, 2018 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .