How do I correctly edit an object in another scene?
I am using additive scenes, then have an editor function to modify a property an another object inside a different scene.
However, it's not saving the change when I hit play!!
Here's the code:
var units = GameObject.FindObjectsOfType<BaseUnit>();
foreach ( var unit in units )
{
if ( unit.IsFirstPickUnit == true )
{
unit.IsFirstPickUnit = false;
if ( !Application.isPlaying )
EditorSceneManager.MarkSceneDirty( unit.gameObject.scene );
}
}
But it doesn't work! The scene is correctly marked dirty, but the unit's flag is NOT changed.
So I tried this next:
var units = GameObject.FindObjectsOfType<BaseUnit>();
foreach ( var unit in units )
{
if ( unit.IsFirstPickUnit == true )
{
unit.IsFirstPickUnit = false;
var serializedObject = new UnityEditor.SerializedObject(unit);
var prop = serializedObject.FindProperty( "IsFirstPickUnit" );
prop.boolValue = false;
serializedObject.ApplyModifiedProperties();
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
if ( !Application.isPlaying )
EditorSceneManager.MarkSceneDirty( unit.gameObject.scene );
}
}
That didn't work either.
What am I doing wrong? It keeps leaving the flag as "true" on any object modified in another scene. (even if I hit save)
Answer by ahmadgml32 · Jun 01, 2020 at 04:58 PM
check this out : https://answers.unity.com/questions/1222872/changing-something-in-another-scene-by-answering-c.html
Your answer

Follow this Question
Related Questions
SceneManager not finding scene at buildIndex, even though there is one. 6 Answers
Scene Manager worked once but not twice 0 Answers
SceneManager.LoadScene is crashing or freezing for Android Devices 3 Answers
Unity Scene Is Loading/Not Loaded on player death 1 Answer
How do you use SceneManager??? 4 Answers