I bought a book to learn Unity development, based on unity 4.
It provide explanation on old GUI management, but I’m on 4.6 and I cannot resolve my problem with new UI management.
The purpose is to create a battery picture (UI) on screen, and manipulate the picture (hide, change texture with 1, 2, 3 or 4 level of charging…).
I created an Image object on the root gameobject (Menu “Gameobject>UI>Image”).
Unity created itself a Canvas, the Image as child (renamed batteryGui), and a Eventsystem object…
I created a JS script to manipulate the Image object, and attached to Image Object “batteryGui”. See Screenshot. I can’t find the way to access in JS to the Image object.
#pragma strict
static var charge : int = 0;
var charge1tex : Texture2D;
var charge2tex : Texture2D;
var charge3tex : Texture2D;
var charge4tex : Texture2D;
var charge0tex : Texture2D;
function Start () {
// = false; // Hide the batteryGui
charge = 0;
}
function Update () {
if(charge == 1){
// = true; // Display the batteryGui
// = charge1tex; // Change the texture to charge1tex
}else
if(charge == 2){
// = charge2tex; // Change the texture to charge2tex
}else
if(charge == 3){
// = charge3tex; // Change the texture to charge3tex
}else
if(charge >= 4){
// = charge4tex; // Change the texture to charge4tex
}else{
// = charge0tex; // Change the texture to charge0tex (empty)
}
}
Could anyone help me to remove the quotes and find the commands ?
batteryGui is not accessible in the Mono prediction…
trying to use “BatteryGui” like “BatteryGui.enable” = false;
=> BCE0005: Unknown identifier: ‘BatteryGui’."
I tried to access to the Image component :
Image batimage = gameObject.GetComponent(Image);
batimage.enable = false;
=> BCE0018: The name ‘Image’ does not denote a valid type (‘not found’)
Image is not a component, not an object type ??? Obviously, I can’t create “var batimage : Image;”
A victory, I’d disabled the canvas with GetComponentInParent(Canvas) and it worked
You have to include the following line on the top of your class file
import UnityEngine.UI;
Unity is on the way to splitting it code up into more name-spaces and the GUI is the first one.
Without the line unity does not load the GUI code and does not now the Image Component.
Yeaaaahhh ! Many thanks !!!
The book (base on older version of unity) does not explain this.
For people who are following the same tutorial :
import UnityEngine.UI; // Force unity to laungh the UI components
#pragma strict
static var charge : int = 0;
var charge1tex : Sprite;
var charge2tex : Sprite;
var charge3tex : Sprite;
var charge4tex : Sprite;
var charge0tex : Sprite;
var batteryGui : Image; // Prepare a var that will contain the Image object created with GameObject>UI>Image
function Start () {
batteryGui = gameObject.GetComponentInChildren(Image); // Find the Image GameObject
batteryGui.enabled = false; // Hide the Image on start
charge = 0;
}
function Update () {
if(charge == 1){
batteryGui.enabled = true; // Display the Image
batteryGui.sprite = charge1tex; // Change the texture to charge1tex
}else
if(charge == 2){
batteryGui.sprite = charge2tex; // Change the texture to charge2tex
}else
if(charge == 3){
batteryGui.sprite = charge3tex; // Change the texture to charge3tex
}else
if(charge >= 4){
batteryGui.sprite = charge4tex; // Change the texture to charge4tex
}else{
batteryGui.sprite = charge0tex; // Change the texture to charge0tex (empty)
}
}
I just see that “Import UnityEngine.UI;” command in this script allow me to manipulate the Image component of batteryGui object in another script. I think I can group Imports in one main script.