0
votes

Before starting the game I have two scenes in the editor :

Game and Main Menu

Before running the game

On the object GameObject at the bottom I have a script Scaling.

If I will drag the Cube to the field Object To Scale and will run the game it will work fine. I can press on the G key and it will scale up/down the cube fine.

Bit if I will drag the NAVI under Player to the Scaling script field Object To Scale then when running the game NAVI is destroyed since now Player (Clone) is on another scene.

The cube is on the same scene when running the game.

Now a screenshot after running the game : The Cube and the GameObject with the Scaling script are still in the Game scene. But the Player is now in the DontDestroyOnLoad scene :

And then NAVI is missing from the Scaling script :

After running the game the Player with the NAVI child are in another scene

Player is prefab maybe I should drag to the script the Player prefab and then in the script to find NAVI child ? Instead dragging the NAVI.

1
I'm not sure if I understand you correctly, but for one time searches GameObject.Find("<name>") could help youLukas

1 Answers

2
votes

It appears that the player object that's in the scene gets destroyed and a new player gets instantiated from a prefab. Since the original player from the scene is destroyed, the reference to the original NAVI is lost as well. If you want the script to reference the NAVI on the cloned player, it would have to be attached to the player prefab or it would have to be searched for and referenced at runtime.

So yes, you should drag it to the NAVI object on the player prefab instead of the player in the scene.