- Home /
Flickering materials/textures when changed by script
I am doing a serious game for a research project and I've run into some problems I can't get my head around.
What I'm trying to make is a display for an elevator that changes between different numbers over time to make it appear as the elevator is moving. I have tried different approaches, switching out textures, materials and even objects to make this display but they all end up the same, a seemingly random flickering between the different numbers.
I am new to scripting but this is what I've been trying to use. The script is supposed to change the material of the specified gameobject, wait for specified time, change the material again and so on.
var timeBetweenFloors : float = 5;
private var idDisplay : GameObject;
function Awake () {
idDisplay = GameObject.Find('display');
}
function Update () {
ElevatorRide();
}
function ElevatorRide () {
idDisplay.gameObject.renderer.material = Resources.Load("display15") as Material;
yield WaitForSeconds (timeBetweenFloors);
idDisplay.gameObject.renderer.material = Resources.Load("display14") as Material;
yield WaitForSeconds (timeBetweenFloors);
idDisplay.gameObject.renderer.material = Resources.Load("display13") as Material;
yield WaitForSeconds (timeBetweenFloors);
idDisplay.gameObject.renderer.material = Resources.Load("display12") as Material;
}
When I run the game this first changes the material of the display to the correct one, "display15", but on the next step it just begins to flicker between all of the mentioned materials in the code at a seemingly random rate.
The result is the same when I use .renderer.material.maintexture to switch out the texture instead. I also tried making a duplicate of the display object for each number and applied different textures to them and switched the rendering of these objects on and off as below but with the same flickering result.
floor16.gameObject.renderer.enabled = false;
floor15.gameObject.renderer.enabled = true;
yield WaitForSeconds (TimeBetweenFloors);
floor15.gameObject.renderer.enabled = false;
floor14.gameObject.renderer.enabled = true;
yield WaitForSeconds (TimeBetweenFloors);
...
Is there something I have done completely wrong in my code? Are the previously used materials/textures supposed to remain visible even after they are changed out?
Is there a better approach to make a display that changes texture/material with specified time intervals?
I'm running the same issue. Did you found the solution ?
Hey mate, I've added an answer not sure if it helps you :)
Answer by jimmycrazyskills · Dec 21, 2018 at 06:51 PM
Hi mate, I'm not completely sure but I think it might be to do with the order of execution.
Order Of Execution:
Elevator Ride is called from Update function (takes a little bit of time).
Material is changed to display15 (takes a little bit of time).
WaitForSecondsIsCalled (takes a little bit of time).
Depending on how much time it's taken to execute these lines, we might get Another Update call come through.
Now ElevatorRide is called again, whilst the previous call is still happening, so you will lose which order the materials change in.
Below is a picture of what I'm trying to explain (the numbers being the order in which each line is executed, each colour represents a new call to ElevatorRide):
My main point here is that the ElevatorRide
function doesn't necessarily complete before a new ElevatorRide
starts so this is why you are seeing a 'Random' order of materials.
Hope this helped explain abit :)