Timy Ash
Timy Ash

Reputation: 107

How to pass variables from one object in one scene to another object in another scene in Unity3D?

Is it possible to pass variables from one object in one scene to another object in another scene? If so, I need to pass unityNameSelected global variable from first script(which is listed bellow) attached in an object in scene 2 to the second script attached to another object in the scene 1. Thanks for any help.

{

        while ( reader.Read() ) 
        {
            if ( reader.NodeType == XmlNodeType.Element ) 
            {
                if ( reader.HasAttributes ) 
                {
                    if ( reader.GetAttribute("UnityName") != null ) 
                    {
                        unityName = reader.GetAttribute("UnityName");
                        if(!values.Contains(unityName))
                        {
                   values.Add(unityName);
                   DontDestroyOnLoad(GameObject.Find("unityName")); 


                         unityNameSelected = unityName;
                    //string[]  unityNameSelected3 = {unityName};
                    //   unityNameSelected =unityNameSelected3;
                           print(unityNameSelected.ToString());

          }
                   }
                            }
                    }
                }}

Upvotes: 2

Views: 1746

Answers (1)

Botz3000
Botz3000

Reputation: 39600

It looks like you already know how to prevent the object from being destroyed by unity (DontDestroyOnLoad). So all you now need to do is store it in some place where it will still be accessible after the next level has loaded. A simple way to do this is a static variable:

public class MyScript : MonoBehaviour
{
    public static GameObject unityNameSelected;
}

Upvotes: 2

Related Questions