I’m trying to do a simple player lives/powerups/whatever display using Unity textures. I want to create one GUI script that takes the variables from other scripts. I was able to succesffuly create a health bar using this method, but I’m hung up on grabbing a variable (player teddypowerup) and displaying it using the other script. Here’s what I have so far, where am I going wrong?
- Create a Gui gameobject and attach this script:
var TeddyTexture : Texture2D; function Update () { guiTexture.texture = TeddyTexturGamecontroller.PowerUpTeddy]; } //@script ExecuteInEditMode
It’s grabbing the variable ‘PowerUpTeddy’ from the GameController script
var player : GameObject; //var guiSkin : GUISkin; var spawnPoint : Transform; public var mainCam : Camera; public var StartSpawn : boolean=true; var pos : Vector2 = new Vector2(20,40); var size : Vector2 = new Vector2(60,20); static var PowerUpTeddy = 1; static var Costume = 0; function Awake() { if(!spawnPoint){spawnPoint= transform;} if(!mainCam){mainCam=Camera.main;} if(StartSpawn){ var curPlayer:GameObject=Instantiate(player, spawnPoint.position, transform.rotation); } mainCam.gameObject.GetComponent(cam).target =curPlayer; var PowerUpTeddy = 1; }
The following code generates a “Array out of scope error” even if I instantiate the script to ‘1’
I’ve also tried to just call the script component directly from the Guiscript using this code:
gameObject.Find("GameController").GetComponent(Gamecontroller);
I’m able to find the script component, but I’m unsure what to do with it. I tried to do this to attach the variable to my texture, but got an error:
GUI.DrawTexture(Rect(600,410,100,100), PowerupTeddy, ScaleMode.ScaleToFit, true, 10.0f.GameController.PowerUpTeddy); //I guess you can't call the variable using this method:
So my long winded question: What is the best way to display GUI elements that rely on variables to display? IE health, current weapon, etc. Thanks in advance for any help.
Side note: I’ve also t