- Home /
How do you update code/stage/screen at runtime?
How would you go about updating the stage at runtime?
For example I was replacing green grids for a different color and they stayed green even thou the code was correct, so I found a workaround where I tell the code to wait 0 Seconds then it worked.
I have the problem again, I destroyed my grid, then recreated my new grid and all my buildings are missing from the grid that was destroyed. I have a grid, had the grid replaced with another grid,
EditTheGrid(); // Here I replace some of the green grids with a different color.
I need something here to update the code/Stage/windows whatever its called.
PlaceBuildings(thePath,loc0,loc1,loc2,loc3); // Here I add the buildings
All the green grids have buildings and non green grids have missing buildings. When I use the print command, the buildings are listed in my list. It just wasn't updated on screen. I am unable to use the wait 0 second routine this time around
if (vBuildTemp == 1) StartCoroutine(MyMethod());
IEnumerator MyMethod() { // Wasn't updating grids, so I added this
yield return new WaitForSeconds(0);
vBuildTemp = 0;
viewBuildings = true;
}
I had the same problems using the Language Director's Lingo and they had a command "UpdateStage" there should be something similar in unity.
For easier understanding if you destroy a game object folder then recreate the game folder it seems like all the objects you created goes into the previous deleted game folder it works if I use the wait for seconds then it created the object in the new folder. Maybe a bug in Unity?
Answer by tanoshimi · May 27, 2014 at 06:29 PM
Any runtime changes should take effect immediately - no "UpdateStage" method is necessary, and I've never heard of a workaround involving waiting 0 seconds. "Folders" are not related to runtime - they're how your project is stored on disk. If you post the actual code that doesn't work we can probably point out the true issue.
Unable to paste all the codes, too large.
EditTheGrid();
// Here i place the grid, and if the grid is not green delete the grid and replace with a different color, buildings with a different grid color are missing, the grids are there
PlaceBuildings(thePath,loc0,loc1,loc2,loc3);
// Here I place the buildings.
public static void PlaceBuildings(GameObject thePath,int loc0,int loc1,int loc2,int loc3) {
string objectType = thePath.transform.parent.gameObject.GetComponent<Info>().sizeFeature.ToString();
int grid$$anonymous$$ax = 1; int rota = 0;
if (objectType == "Giant") grid$$anonymous$$ax = 27;
if (objectType == "Planet") grid$$anonymous$$ax = 19;
if (objectType == "SmallPlanet")grid$$anonymous$$ax = 13;
if (objectType == "Dwarf") grid$$anonymous$$ax = 07;
if (objectType == "Satellite") grid$$anonymous$$ax = 04;
if (objectType == "Asteroid") grid$$anonymous$$ax = 01;
for (int grid = 0; grid <= grid$$anonymous$$ax; grid++) {
for (int area = 0; area <= 6; area++) {
if ($$anonymous$$yScript.gridLocation[loc0,loc1,loc2,loc3,grid,area] != 0) {
int i = 1;
while(i <= 8) {if (gridLocation[loc0,loc1,loc2,loc3,grid,area] == i) buildingPrefab = Resources.Load("Station" + i, typeof(Transform)) as Transform; i++;}
List<int> rotaList = new List<int>(new int [] {90,150,210,270,330,30,90}); i = 0;
foreach(int item in rotaList) {if (area == i) rota = item; i++;}
Transform newBuilding = (Transform) Instantiate(buildingPrefab, new Vector3(0, 0, 0), Quaternion.identity);
newBuilding.transform.parent = GameObject.Find("$$anonymous$$ain Camera/Build$$anonymous$$ode/Grid/" + grid + "/" + area).transform;
newBuilding.transform.name = gridLocation[loc0,loc1,loc2,loc3,grid,area].ToString();
foreach (Transform child in newBuilding.transform)
{
child.gameObject.layer = 0;
if (child.gameObject.collider) child.gameObject.collider.enabled = false;
foreach (Transform grandChild in child){
grandChild.gameObject.layer = 0;
if (grandChild.gameObject.collider) grandChild.gameObject.collider.enabled = false;
}}
newBuilding.transform.localPosition = new Vector3(0,0,0);
newBuilding.transform.localRotation = Quaternion.Euler(new Vector3(0,rota,0));
newBuilding.transform.localScale = new Vector3(0.60f, 0.60f, 0.60f);
}}}}
Here is an example replacing the grid. If I had something like updateStage then this code would work
// Blue Aqua Farm
count = gridColor0 + gridColor1;
for (int p = 0 + count; p <= -1+gridColor2 + count; p++){
Transform newGrid = (Transform) Instantiate(gridBlue, new Vector3(0, 0, 0), Quaternion.identity);
var oldObj = GameObject.Find("$$anonymous$$ain Camera/Build$$anonymous$$ode/Grid/" + p);
newGrid.transform.parent = oldObj.transform.parent;
newGrid.transform.rotation = oldObj.transform.rotation;
newGrid.transform.position = oldObj.transform.position;
GameObject.Destroy(oldObj);
newGrid.transform.name = p.ToString();
}
I changed the top script to this. Ins$$anonymous$$d of deleting and recreating I just changed the color of the object. Currently I deleted game object folder then I recreated a new game object folder, then I added the building inside the script, but only it created the building inside the deleted game object folder. This is not an answer just a work around, like the other script I had to use a wait timer for a work around.
List<string> gridList = new List<string>(new string [] {"0","1","2","3","4","5","6","Border","Background"});
// Blue Aqua Farm
count = gridColor0 + gridColor1;
for (int p = 0 + count; p <= -1+gridColor2 + count; p++){
foreach(string item in gridList) {
var obj = GameObject.Find("$$anonymous$$ain Camera/Build$$anonymous$$ode/Grid/" + p + "/" + item);
if (item != "Background") obj.renderer.material.color = new Color32(0,0,255,255);
if (item != "Background") obj.renderer.material.SetColor("_Emission", new Color32(0,0,255,255));
if (item == "Background") obj.renderer.material.color = new Color32(0,0,50,150);
}}
Your answer
Follow this Question
Related Questions
Unity is not updating my scripts 2 Answers
Expecting ( found Update (Javascript) 1 Answer
Code isn't Recursive in update? 2 Answers
Coroutine - events not working 2 Answers
How to detect empty slot and spawn gameobject there? 1 Answer