- Home /
A Button, A Boolean and 2 Textures.
Well. I've tried to make a Button that changes the Texture whaen Pressed and Sets a variable to True of False. So, this don't get right.
using UnityEngine;
using System.Collections;
public class DayNightSelection : MonoBehaviour {
public static bool night = false;
public Texture2D NightTexture = Assets/Textures/GUI/Night;
public Texture2D DayTexture = Assets/Textures/GUI/Day;
// Update is called once per frame
void Update () {
if (night = true)
{
guiTexture.texture = NightTexture;
}
else
{
public static bool night = DayTexture;
}
}
void OnMouseDown() {
if (night = true)
{
public static bool night = false;
}
else
{
public static bool night = true;
}
}
}
Well... That's it. I want to use the Night Variable as public to define if the Unity will load the Day or the Night Version when you pressed the button to Play.
Answer by PokeJoe · Aug 13, 2014 at 02:25 AM
First thing I notice is this:
if (night = true)
Remember that = and == are not the same thing. You are assigning night every time you do this, not testing it.
Answer by Yinja · Aug 13, 2014 at 02:31 AM
Hi lebuildman,
I think you want to change the if statement, right now it's usually 1 equal sign instead of two. So it's actually assigning the variable night to be true on that if statement. Also you don't need to change the texture on every frame via Update. You can do it OnMouseDown().
I think you want something like this:
using UnityEngine;
using System.Collections;
public class DayNightSelection : MonoBehaviour {
public static bool night = false;
public Texture2D NightTexture = Assets/Textures/GUI/Night;
public Texture2D DayTexture = Assets/Textures/GUI/Day;
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
if (night == true)
{
night = false;
guiTexture.texture = DayTexture;
}
else
{
night = true;
guiTexture.texture = NightTexture;
}
}
}
Side note: you could remove night bool variable from the code all together. Instead check what the current texture is OnMouseDown() and change to the other texture.
Your answer
Follow this Question
Related Questions
Assiging an Image to a Button 1 Answer
Make a Button out of a Textured Plane 2 Answers
Buttons and images 1 Answer
GUI Button Texture only showing as small 1 Answer
GUI buttons look fine on full screen but distorted otherwise? 0 Answers