- Home /
Tranferring Color from a script to a Gui Box?
The following script is intended to transfer a Color from a variable in another script and 'visualise' that color by means of the GUI. Code below.
Help would be appreciated.
Gary
//this script reads the public colour variable from a GameObject.Script.Variable (dataField1.DataField.ColorUnderCharacter) and puts it on a square Box GUI.
var plainTex : Texture2D ;
var rayTexColor : Color = Color.white ;
var searchGameObject : GameObject ;
var searchScript : GameObject ;
function Start()
{
var accessGameObject = GameObject.Find("dataField1") ;
var accessScript = accessGameObject.GetComponent("DataField")
}
function Update ()
{
rayTexColor = accessScript.ColorUnderCharacter ;
print(rayTexColor.grayscale) ;
}
function OnGUI() {
//Find dataField data and set it to the current colour to paint the GUI
//Configure the Box, Draw Box, reset GUI
var c : Color = GUI.backgroundColor ;
GUI.backgroundColor = rayTexColor ;
GUI.Box(Rect(10,10,50,50)) ;
GUI.backgroundColor = c ;
}
Is it giving an error, if so what line? Or just not working, if so, how not?
Sorry for being slow... error codes are:
Assets/$$anonymous$$ap Reading/Scripts/ColouredSquare.js(15,23): BCE0005: $$anonymous$$ identifier: 'accessScript'.
Assets/$$anonymous$$ap Reading/Scripts/ColouredSquare.js(24,16): BCE0023: No appropriate version of 'UnityEngine.GUI.Box' for the argument list '(UnityEngine.Rect)' was found.
You need a semicolon at the end of the last line of the Start function. GUI.Box needs at least two params, try putting "" (empty string) after the Rect definition. of GUI.Box
Answer by DaveA · Mar 31, 2011 at 09:12 PM
Hmm. I'm not sure setting backgroundColor would do what you want. For one, it multiplies this color, tinting things. And I don't know that setting and resetting it like this will do it for just that one element, maybe. I personally would look into using GUIStyle for this type of thing. It's a lot like CSS.
Thanks - saw this somewhere with a comment saying that it worked. Will try for a bit. Appreciate the support
Your answer
![](https://koobas.hobune.stream/wayback/20220613120037im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
variable before (dot) 2 Answers
Get variable from another object 1 Answer
Any problems with this script? 1 Answer
Changing size of variable 1 Answer
GetComponent Help 2 Answers