Too subjective and argumentative
Changing Texture after time has passed
Hey there - Unity noob here. So I'm working on a horror game, where the player uses a smartphone as a flashlight. The flashlight has a time limit of 60 seconds. I want to change the texture of the screen on the smartphone to display a "battery" level as the time decreases. I have written some code but it does not work, but I think will provide the basic idea of how I want it to work and am looking for some guidance. Thanks in advance!
Here's my code: public class SmartphoneTextureManager : MonoBehaviour {
public Texture[] SmartphoneTexture;
public float textureCoolDown = 60;
public bool EnableChange = false;
public Renderer renderer;
// Start is called before the first frame update
void Start()
{
renderer.material.mainTexture = SmartphoneTexture[0];
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown("E"))
{
textureCoolDown -= Time.deltaTime;
EnableChange = true;
}
if(textureCoolDown > 50)
{
renderer.material.mainTexture = SmartphoneTexture[1];
}
if (textureCoolDown > 40)
{
renderer.material.mainTexture = SmartphoneTexture[2];
}
if (textureCoolDown > 20)
{
renderer.material.mainTexture = SmartphoneTexture[3];
}
if (textureCoolDown > 10)
{
renderer.material.mainTexture = SmartphoneTexture[4];
}
else
{
renderer.material.mainTexture = SmartphoneTexture[0];
}
}
}
Notes: In the array, [0] represents the normal texture, [1] represents green battery, [2] yellow battery, and so forth.
Answer by tormentoarmagedoom · Mar 10, 2020 at 01:19 PM
Hello.
You should do it with "else if" not all if. As ypu use if everywhere, code is checking all of them.
if
else if
else if
else if
else
@tormentoarmagedoom Thanks for your comment, however this does not change anything. I'm starting to wonder if I may have planned this incorrectly in the inspector. Also, for some reason my 'textureCoolDown' does not count down from 60 when 'E' is pressed. Any idea?
Hello again.
I think you need so basic learning...
with this code
if(Input.Get$$anonymous$$eyDown("E"))
{
textureCoolDown -= Time.deltaTime;
EnableChange = true;
}
its only executed the frame you start pressing the E key. Only 1 frame...
Go back learn basics of Unity progra$$anonymous$$g.
Follow this Question
Related Questions
My code has some invalid arguments 1 Answer
Render Pipeline Material Issue 1 Answer
How Can I Create My Own Textures? 3 Answers
Change the size of a GameObject in a certain timespan. 0 Answers
Textures, Materials, Meshs and Maps? 1 Answer