- Home /
Having issues with C# flashlight script (CS0117:)
Before i updated this morning it was working as intended, but i want to display an image in the upper left of the screen. Basic image letting the player know FLash light Off(Gray Out) and Flashlight Active(Full colored).
I created the images already, but i cannot seem to get it to let me add them, i am used to the java now so if anyone can point out what i "Fubar'ed" i would appreciate it.
Error: CS0117:
UnityEngine.GUI' does not contain a definition for
Texture' I forgot to note: It's on Lines, 45,48,49
Here is the code, i tried several variations, like toggle it On/Off, and jusr turn one of and the other off...
using UnityEngine;
using System.Collections;
public class FlashLight : MonoBehaviour
{
public Texture2D image;
public Texture2D image2;
public Light FlashLightObject;
private bool LightEnabled = false;
private bool ShowHide = true;
private bool ShowHide2 = false;
void Update ()
{
if(Input.GetButtonDown("Flashlight"))
{
LightEnabled = !LightEnabled;
FlashLightObject.enabled = LightEnabled;
ShowHide = GUI.Texture (new Rect (10,10,image.width,image.height),image);
//ShowHide.SetActiveRecursively(!ShowHide.active);
audio.Play();
if(!LightEnabled = false){
ShowHide2 = GUI.Texture (new Rect (10,10,image2.width,image2.height),image2);
//ShowHide2.SetActiveRecursively(!ShowHide2.active);
}
}
}
}
Answer by zombience · Dec 06, 2013 at 08:00 PM
Updated ah, you probably want to use a GUITexture then. http://docs.unity3d.com/Documentation/ScriptReference/GUITexture.html
void Update()
{
// non gui stuff
}
void OnGUI()
{
// all your gui stuff
}
Original you seem to be reassigning your boolean value LightEnabled. I wonder if this is causing your script to always evaluate it to false?
if(!LightEnabled = false) // right here is a problem
{
ShowHide2 = GUI.Texture (new Rect(10,10,image2.width,image2.height),image2);
//ShowHide2.SetActiveRecursively(!ShowHide2.active);
}
you actually don't need to compare the boolean to false. you can just evaluate the bool, like so:
if(!LightEnabled)
{
// do stuff
}
to clarify, you intended to write:
if(!LightEnabled == false)
I fixed that, but the issue is it jkeeps saying:
(45,16): error CS0117:
UnityEngine.GUI' does not contain a definition for
Texture'
Ok i updated it, -GUI.Texture to GUITexture, now that error is gone but now it says...
CS0119: Expression denotes a
type', where a
variable',value' or
method group' was expected
This is annoying, i don't know what i need to change now.
i believe you need to move your GUI calls into OnGUI()
for example:
void Update()
{
// non gui stuff
}
void OnGUI()
{
// all your gui stuff
}
OnGUI is a method that unity uses to process gui calls on gameobjects. you can use it like update, but put all of your GUI work in there. try that. if that isn't working for you, you should consider using GUITexture on a separate game object, and link that to your current script. then you can access it's texture via: GUITexture.texture
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Terrain texture low quality 0 Answers
Memory issues when taking Screenshots ? 1 Answer
rotate a texture inside a cube 1 Answer
Stuck at "Import EntityScene" 0 Answers