Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[UI] Cannot access image object in js

Discussion in 'UGUI & TextMesh Pro' started by Elievar, Jan 11, 2015.

  1. Elievar

    Elievar

    Joined:
    Dec 13, 2014
    Posts:
    5
    Hello,

    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.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. static var charge : int = 0;
    4.  
    5. var charge1tex : Texture2D;
    6. var charge2tex : Texture2D;
    7. var charge3tex : Texture2D;
    8. var charge4tex : Texture2D;
    9. var charge0tex : Texture2D;
    10.  
    11. function Start () {
    12.        // = false; // Hide the batteryGui
    13.     charge = 0;
    14. }
    15. function Update () {
    16.     if(charge == 1){
    17.         // = true; // Display the batteryGui
    18.         // = charge1tex; // Change the texture to charge1tex
    19.     }else
    20.         if(charge == 2){
    21.             // = charge2tex; // Change the texture to charge2tex
    22.         }else
    23.             if(charge == 3){
    24.                 // = charge3tex; // Change the texture to charge3tex
    25.             }else
    26.                 if(charge >= 4){
    27.                     // = charge4tex; // Change the texture to charge4tex
    28.                 }else{
    29.                         // = charge0tex; // Change the texture to charge0tex (empty)
    30.                 }
    31. }
    Could anyone help me to remove the quotes and find the commands ?
    batteryGui is not accessible in the Mono prediction...

    Thanks in advance,
    Elievar
     

    Attached Files:

  2. Elievar

    Elievar

    Joined:
    Dec 13, 2014
    Posts:
    5
    I tried a lot of methods :

    - 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 :)

    But noting to do with the Image object...

    Thanks again for your help guys.
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You have to include the following line on the top of your class file
    Code (JavaScript):
    1. 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.
     
    ZMarco and Elievar like this.
  4. Elievar

    Elievar

    Joined:
    Dec 13, 2014
    Posts:
    5
    Yeaaaahhh ! Many thanks !!!
    The book (base on older version of unity) does not explain this.

    For people who are following the same tutorial :
    Code (JavaScript):
    1. import UnityEngine.UI; // Force unity to laungh the UI components
    2.  
    3. #pragma strict
    4.  
    5. static var charge : int = 0;
    6.  
    7. var charge1tex : Sprite;
    8. var charge2tex : Sprite;
    9. var charge3tex : Sprite;
    10. var charge4tex : Sprite;
    11. var charge0tex : Sprite;
    12.  
    13. var batteryGui : Image; // Prepare a var that will contain the Image object created with GameObject>UI>Image
    14.  
    15. function Start () {
    16.        batteryGui = gameObject.GetComponentInChildren(Image); // Find the Image GameObject
    17.        batteryGui.enabled = false; // Hide the Image on start
    18.        charge = 0;
    19. }
    20. function Update () {
    21.     if(charge == 1){
    22.         batteryGui.enabled = true; // Display the Image
    23.         batteryGui.sprite = charge1tex; // Change the texture to charge1tex
    24.     }else
    25.         if(charge == 2){
    26.             batteryGui.sprite = charge2tex; // Change the texture to charge2tex
    27.         }else
    28.             if(charge == 3){
    29.                 batteryGui.sprite = charge3tex; // Change the texture to charge3tex
    30.             }else
    31.                 if(charge >= 4){
    32.                     batteryGui.sprite = charge4tex; // Change the texture to charge4tex
    33.                 }else{
    34.                         batteryGui.sprite = charge0tex; // Change the texture to charge0tex (empty)
    35.                 }
    36. }
    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.

    Thanks again !

    Battery-Gui, Battery_Gui
     
    Last edited: Jan 12, 2015