Reputation: 7304
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?
Upvotes: 1
Views: 64
Reputation: 7304
I found the way, I need to search the child of the GameObject.
List<GameObject> activeObjects = new List<GameObject>();
scene.GetRootGameObjects( activeObjects );
for (int i = 0; i < activeObjects.Count; ++i)
{
GameObject gameObject = activeObjects[ i ];
Debug.Log (gameObject.name);
if (gameObject.name == "Canvas") {
foreach (Transform firstchild in gameObject.transform) {
Debug.Log ("firstchild name " + firstchild.name);
if (firstchild.name == "Swinginfo") {
foreach (Transform secondchild in firstchild.transform) {
if (secondchild.name == "Clubdigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//result[0].ToString();
}
else if (secondchild.name == "Balldigit") {
secondchild.GetComponent<Text>().text = 10.ToString ();//result[0].ToString();
} else if (secondchild.name == "Distancedigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//0.ToString ();
}
}
}else if(firstchild.name == "Swinginfo2"){
foreach (Transform secondchild in firstchild.transform) {
if (secondchild.name == "Ballspeeddigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//result[0].ToString ();
} else if (secondchild.name == "Distancedigit2") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//0.ToString ();
} else if (secondchild.name == "Backspindigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//12.ToString ();
} else if (secondchild.name == "Sidespindigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//13.ToString ();
}else if (secondchild.name == "Launchangleindigit") {
secondchild.GetComponent<Text> ().text = 10.ToString ();//result[3].ToString ();
}
}
}
}
break;
}
}
EDIT: According to the suggestion, my latest update is
if (scene.name == "RangeView")
{
List<String> objectsToChange = new List<String> () {
"Balldigit",
"Distancedigit",
"Ballspeeddigit",
"Distancedigit2",
"Backspindigit",
"Sidespindigit",
"Launchangleindigit"
};
List<GameObject> activeObjects = new List<GameObject>();
scene.GetRootGameObjects( activeObjects );
for (int i = 0; i < activeObjects.Count; ++i)
{
GameObject gameObject = activeObjects[ i ];
Debug.Log (gameObject.name);
if (gameObject.name == "Canvas")
{
foreach (Transform firstchild in gameObject.transform) {
Debug.Log ("firstchild name " + firstchild.name);
if (firstchild.name == "Swinginfo") {
foreach (Transform secondchild in firstchild.transform) {
Debug.Log ("secondchild name " + secondchild.name);
if (objectsToChange.Contains(secondchild.name))
{
secondchild.GetComponent<Text>().text = 10.ToString();
}
}
}else if(firstchild.name == "Swinginfo2"){
foreach (Transform secondchild in firstchild.transform) {
Debug.Log ("secondchild name " + secondchild.name);
if (objectsToChange.Contains(secondchild.name))
{
secondchild.GetComponent<Text>().text = 10.ToString();
}
}
}
}
break;
}
}
}
Upvotes: 0
Reputation: 5108
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();
}
}
}
Upvotes: 1