Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
gameObject Transform in DontDestroyOnLoad

I use the following script to copy the transform of a gameObject.

public Transform alignment;

void Update()
{
    DontDestroyOnLoad(transform.gameObject);

    transform.position = new Vector3(alignment.position.x, alignment.position.y, alignment.position.z);
    float y = alignment.transform.localRotation.eulerAngles.y;
    float x = alignment.transform.localRotation.eulerAngles.x;
    float z = alignment.transform.localRotation.eulerAngles.z;
    transform.localEulerAngles = new Vector3(x, y, z);
}

I want to acces this transform in other scenes (apply it to gameObjects). How can I reach the transforms, that are in DontDestroyOnLoad?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to give alignment as parameter to the DontDestroyOnLoad method, not transform.gameObject.

DontDestroyOnLoad(alignment.gameObject);

Also, you don't need to call this method inside Update(). Just calling it once it enough. You can call it inside Start(), like this:

void Start()
{
    DontDestroyOnLoad(alignment.gameObject);
}

Now your alignment gameobject will still be accessible when you change scenes. If you want to access it from other gameobjects in the new scene, you might want to give it a tag or make it a singleton.

over 4 years ago · Santiago Trujillo Relatório

0

First of all, don't use DontDestroyOnLoad in your update function. Use it in your Start or Awake funciton.

Secondly you can access your Non-Destroyed object like you would any other object. Only differences between the two is that the Non-Destroyed stays in the scene when changing scenes.

Here are some ways to access it:

var obj = GameObject.Find("My Non Destroyed Object Name");

Or my personal favorite, if the object is used for scoring or settings I'll create a new gameobject and add the following script to it:

public class GameManager : MonoDevelop {

    /* These are all script on the same gameobject that is not being destroyed */
    public static Settings settings;
    public static PlayerMananger playerMananger;
    public static UIManager uiManager;
    
    
    void Start() {
        DontDestroyOnLoad(this.gameobject);
        
        // Get the components
        settings = GetComponent<Settings>();
        playerMananger = GetComponent<PlayerMananger>();
        uiManager = GetComponent<UIManager>();
    }   
}

Now I can create a script from anywhere and I can access all these Manager and setting files simply using:

var settings = GameManager.settings;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda