Scene change and creative cube game object
After saving the pre-stored r and specific values of the Sentence array in the Input_data array,
change the scene to Equip3D, and then use the values stored in Input_data to create a gameobject cube(Input_data array is used for position and size)
At this time, the scene is switched, but objects are not created after that.
Even if I take a log, it runs without any error
Can I know how to fix it?
Here are source code:
public void Find_Out(){
for (int j=0; j<r; j++){
for (int k=0; k<7; k++){
Input_data [j,k]=Sentence[(int)Input_i[j],k+4];
Debug.Log("j= " + j +" k= " + k + " Item= " + Input_data[j,k]);
}
}
SceneManager.LoadSceneAsync("Equip3D");
for (int i=0; i<r; i++){
GameObject equipment = GameObject.CreatePrimitive(PrimitiveType.Cube);
equipment.name=Input_data[i,0]; //장비 이름
equipment.transform.position = new Vector3(float.Parse(Input_data[i,1]), float.Parse(Input_data[i,3]), float.Parse(Input_data[i,2])); //y 대칭
equipment.transform.localScale = new Vector3(float.Parse(Input_data[i,4]), float.Parse(Input_data[i,6]), float.Parse(Input_data[i,5])); //z 대칭
Debug.Log("Scene Change!!!");
}
}
Answer by rh_galaxy · Oct 17, 2021 at 05:20 AM
I think you should wait with the object creation until the scene has loaded to 90%.
bool bBeginCreatingObjects = false;
bool bDoneCreatingObjects = false;
bool bBeginLoadScene = true; //begins immediately, you can remove this and do it in a state machine
bool bDoneLoadScene = false;
string szToLoad = "Equip3D";
void Update()
{
if(bBeginLoadScene)
{
bDoneLoadScene = false;
bBeginCreatingObjects = false;
bDoneCreatingObjects = false;
bBeginLoadScene = false;
StartCoroutine(LoadAsyncScene());
}
if(bBeginCreatingObjects && !bDoneCreatingObjects)
{
//here you have the chance to not create all objects the same frame
// you can split the for loop into different states in a state machine
for (int i=0; i<r; i++)
{
GameObject equipment = GameObject.CreatePrimitive(PrimitiveType.Cube);
//...
}
bDoneCreatingObjects = true;
}
}
IEnumerator LoadAsyncScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(szToLoad, LoadSceneMode.Single);
asyncLoad.allowSceneActivation = false;
//wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
//scene has loaded as much as possible, the last 10% can't be multi-threaded
if (asyncLoad.progress >= 0.9f)
{
bBeginCreatingObjects = true;
if (bDoneCreatingObjects)
asyncLoad.allowSceneActivation = true;
}
yield return null;
}
bDoneLoadScene = asyncLoad.isDone;
}
Your answer
Follow this Question
Related Questions
Need some guidance for my first game! 0 Answers
How to store all components of a GameObject in an array? 0 Answers
Value doesn't change in game when updated. 2 Answers
Destroying game object 0 Answers