- Home /
How to keep the same gameobject with different transform values in different scenes
What I am trying to do is the have a trigger object at the end of different levels and when the player gets inside an animation plays, and then the next scene is loaded.
problem is when I add components like animator, when the scene is changed the reference to the component is missing, it is showing in the inspector and in the debug.log, but error log says it is destroyed.
and I cant use dontdestroyonload bc the position of the trigger will be different in all levels.
How can I manage this? Thanks
Answer by rh_galaxy · Apr 06 at 01:45 AM
You should do the animation before you load the new scene, at the end of the current scene. You can also have a GameManager set as DontDestroyOnLoad, that keeps the camera and does the animation...
You can have the same prefab Instantiated with different transforms in different scenes, but not the same gameobject. You need to show the code if you want more help.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DoorToNextLevel : MonoBehaviour {
Animator anim;
PlayerMovement player;
float t;
GameObject lift;
private void Awake()
{
lift = Instantiate(GameAssets.Instance.lift, this.transform.position, Quaternion.identity, this.transform);
player = FindObjectOfType<PlayerMovement>();
anim = lift.GetComponent<Animator>();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.GetComponent<PlayerMovement>())
{
EnterLift();
}
}
void EnterLift()
{
anim.Play("Lift");
player.transform.parent = lift.transform;
}
}
I got it to working, interesting thing is that I was doing this, only difference was that I had a piece of code that referenced PlayerMovement script(it basically made the player go to inside lift when player hit the trigger), and then enter the lift function was called. so that means reference to the player animation was missing? and actually I instantiate the stuff that I want to keep in the scene with additive loading, so I never destroy them, but I think when I parent and deparent player to lift it changes on the scene and the animator gets destroyed because it was in the second scene after I deparent it from the lift which also was in the second scene. it is probably just a order issue on my side. I will fix it.
Which brings to my question, what do you think is the best design pattern to manage scenes?
Ok, I didn't get that you load additively. So my answer isn't so useful fore you. But to answer your last question
I have only used scenes in my game like this:
1. GameManager and Camera - Singleton DontDestroyOnLoad (created in Menu-scene when game starts)
2. Menu-scene (normal load)
3. Level-scene (normal load) (win / lose / quit to menu -> repeat from 2)
Now I only play one level at a time, but to pass data from scene to scene I keep data in the GameManager during loading a scene then pass it to the objects in the new scene after load.
So I would not load additively, and let the objects be destroyed when unloaded and then recreated with the properties kept with Instantiate() and an Init() function that I create that takes all properties as parameters.
But there are many ways to do things...
Your answer
Follow this Question
Related Questions
Why do I loose my Animator? 0 Answers
Canvas will only appear on the game screen when its child of a new loaded scene canvas 1 Answer
GUI Elements Carry Over To Next Scene 1 Answer
Don't destroy On Load make object not interactable in new scene 0 Answers
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer