- Home /
Assign GUITexture in scene to variable via scripting?
Ok guys what am I doing wrong here? And what am I suppose to do?
Since I have over 60 levels so far where I need to drag and drop 3 in-scene gameobjects, two being textures and one being a gameobject, its gonna be a real bother doing it in each level with drag and drop. So how do I assign them with a script at start?
Here's one of them, and the way I tried but nothing happened:
var fuelMeter : GUITexture;
function Start()
{
fuelMeter = GameObject.Find("FuelMeter/Fuel").GetComponent(GUITexture);
}
So the question is, how do I, by name, assign the GUITexture called Fuel, which is a child of FuelMeter, to the variable fuelMeter which is a GUITexture?
No, like I said, all the objects I wanna assign are in that scene and since I have only one of each its fine if I do it by name, I just don't know how.
Answer by robertbu · Sep 27, 2013 at 12:24 AM
How about:
var fuelMeter : GUITexture;
function Start() {
var go = GameObject.Find("FuelMeter"); // Find the parent game object
var fuel = go.transform.Find("Fuel"); // Get the transform of the child
fuelMeter = fuel.guiTexture; // Get the guiTexture from child
}
Thanks for the answer but nope, nothing happens, it doesn't assign it.
Note I tested the above code before I posted it. I had a game object called 'Fuel$$anonymous$$eter' with a child object called 'Fuel' that had a GUITexture component. It worked fine. Put some debug.log statements in your code to figure out what is going on. Does it find 'Fuel$$anonymous$$eter'? Does it find 'Fuel'? Is guiTexture found on fuel? Do you have more than one game object that has the name 'Fuel'? Are the strings you are using an exact match to the name of the game objects?
Yes your script should work but nothing happens. I have an empty game object called Fuel$$anonymous$$eter, and two childs one Fuel(GUITexture), and the other unimportant and just there for visual purposes, called Fuel $$anonymous$$eter(GUITexture), with a space.
As mentioned in my previous comment, what have you done to track the problem down? Put Debug.Log() statements and verify each part of the code. $$anonymous$$ake sure it is finding the correct 'Fule$$anonymous$$eter'. Start with an empty scene with just your Fuel$$anonymous$$eter and this script does it work for you? If the code above is not working, then there is something else going on that needs to be tracked down.
Thank you! Your code was perfectly fine it just had to go before a couple lines related to the fuel meter that prevented it from happening. And as for assigning a game object in the scene to a GameObject variable I can use the same way I assume. Thanks again this will make everything a whole lot easier! :D
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to emit particle from a collision? 2 Answers
getting a sum from two scripts 1 Answer
Rotating a Sphere with the mouse? 1 Answer