Why when creating a ScriptableObject class I can't drag a GameObject/Transform to it in the Inspect

The idea is to use the player GameObject variable in two scenes.

This is the script:

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

[CreateAssetMenu]
public class PlayerScriptable : ScriptableObject
{
    public GameObject player;
}

Then in the editor I did in the Assets: Right click > Create > Player Scriptable:
The result is:

I have two scenes the first is Main Menu and this is index 0 in the Build Settings…
The second scene is The Space Station index 1 in the Build Settings…

What I want to do is to be able to access Player ( You can see the Player in the Hierarchy on the left at the bottom).

Now Player is turned off disabled.
In the Main Menu scene I have this script attached to a button On Click:

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

public class LoadSceneOnClick : MonoBehaviour
{
    public void ActivatePlayer()
    {
        SceneManager.UnloadSceneAsync(0);
    }
}

In this script I want to access Player and enable it on true.
But Player is in the The Space Station scene , So I need to pass it to the Main Menu scene.

This is how the StartGameButton looks like on the right the button On Click:

The problem is when I try to drag Player to the Player in the inspector it’s just not allowed. I can’t.

How can I interact with Player in both scenes to turn Player on/off ?

The player doesn’t exist in the first scene so you cannot interact with it. If you leave the player on, it will start enabled when you start the space station scene.

1 Like

You can’t reference a scene object from a prefab asset. scene objects only exist in memory at runtime, whereas prefabs persist in memory. as such, you’d need some way to dynamically grab the reference to the player object and pass it to the asset at runtime.

Something in a project asset (like your scriptable object) can not hold references to something in a scene (like your player) during edit time.
I would reverse the dependency you’re trying to accomplish. Instead of having a direct reference to Player from the scriptable object, let the Player have a reference to the scriptable object. On that SO, keep track of for example a playerIsActive bool, then have Player check that bool when the scene loads.

1 Like

Ok I solved it this way and it’s working but I wonder if this is a good way to do it ?
I’m not using the ScriptAbleObject at all.

I created a new script:
This script is attached to new empty GameObject on the second scene where the Player is.

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

public class PlayerEnable : MonoBehaviour
{
    public GameObject player;
    public static bool playerStat = false;

    private void Update()
    {
        if (LoadSceneOnClick.isPlayer == true)
        {
            player.SetActive(true);
            playerStat = true;
        }
    }
}

And in the Main Menu scene I’m using this script on the button:

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

public class LoadSceneOnClick : MonoBehaviour
{
    public static bool isPlayer = false;

    public void ActivatePlayer()
    {
        isPlayer = true;

        if (PlayerEnable.playerStat == true)
            SceneManager.UnloadSceneAsync(0);
    }
}

Now when the game is running and I click on the StartGameButton it’s activating the Player and unloading the main menu scene.

Is that logic way to do it ?