Search code examples
c#unity-game-engineunity3d-2dtools

How to edit Active scene's GameObject from another script?


I am making a Unity project and I need to edit Text of some UI of current active scene from another different script (not linked to that active scene).

What I do is

            Scene scene = SceneManager.GetActiveScene();
            Debug.Log (scene.name);
            if (scene.name == "RangeView") 
            {
                List<GameObject> activeObjects = new List<GameObject>();
                scene.GetRootGameObjects( activeObjects );
                for (int i = 0; i < activeObjects.Count; ++i)
                {
                    GameObject gameObject = activeObjects[ i ];
                    if (gameObject.name == "Clubdigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } 
                    else if (gameObject.name == "Balldigit") {
                        gameObject.GetComponent<Text>().text = 10.ToString ();
                    } else if (gameObject.name == "Distancedigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Ballspeeddigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Distancedigit2") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Backspindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    } else if (gameObject.name == "Sidespindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    }else if (gameObject.name == "Launchangleindigit") {
                        gameObject.GetComponent<Text> ().text = 10.ToString ();
                    }
                }

            }

The update is not reflected on the scene. How can I update to the active scene?


Solution

  • I see you found the solution, great. It would be awesome if you would share the solution here in Stackoverflow, so that anyone else with similar issues can use your solution to help them. As a treat, here's some refactoring of your code to make it easier to manage and read, should you wish.

    // Put the dependency Using System.Linq; at the top
    
    List<String> objectsToChange = new List<String>() 
    {
      "Balldigit",
      "Distancedigit",
      "Ballspeeddigit",
      "Distancedigit2",
      "Backspindigit",
      "Sidespindigit",
      "Launchangleindigit"
    }
    
    Scene scene = SceneManager.GetActiveScene();
    Debug.Log (scene.name);
    
    if (scene.name == "RangeView") 
    {
        List<GameObject> activeObjects = new List<GameObject>();
        scene.GetRootGameObjects( activeObjects );
        foreach (GameObject activeObject in activeObjects)
        {
            if (objectsToChange.Contains(activeObject.name)) 
            {
              activeObject.GetComponent<Text>().text = 10.ToString();
            }
        }
    
    }