• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Sandman1980 · Mar 28, 2014 at 09:03 PM · error message

Implicit downcast from 'Object[]' to 'UnityEngine.GUITexture[]'

Hi, i am very noob in Unity and in programming. I try to follow some tutorials but in one point of the track i get stuck with this error: Implicit downcast from 'Object[]' to 'UnityEngine.GUITexture[]'. InvalidCastException: Cannot cast from source type to destination type. Showlives..ctor () (at Assets/Juego Shooter/Scripts/Showlives.js:4)

I see that the problem is in the line 4 of my code but i don´t know what are wrong.

 #pragma strict
 
 var livetexture:Texture;//La textura que corresponde a las vidas restantes
 var lives:GUITexture[]=[null,null,null];//el numero de vidas
 var texto:GUIText;//El texto que indica fin de juego
 var texto2:GUIText;//El texto que indica las instrucciones
 
 function Update () {
     
     if(Globals.Lives==3)
     {
     lives[2].color.a=128;
     lives[1].color.a=128;
     lives[0].color.a=128;
     }
     
     if(Globals.Lives==2)
     {
     lives[2].color.a=0;
     lives[1].color.a=128;
     lives[0].color.a=128;
     }    
 
     if(Globals.Lives==1)
     {
     lives[2].color.a=0;
     lives[1].color.a=0;
     lives[0].color.a=128;
     }
 
     if(Globals.Lives==0)
     {//cuando el numero de vidas llega a 0, se muestra pantalla Game Over y si pulsamos boton asignado vamos a menu principal
     lives[2].color.a=0;
     lives[1].color.a=0;
     lives[0].color.a=0;
     texto.text="Game Over";
     texto2.text="Pulsa ESPACIO para Continuar";
         if(Input.GetButtonDown("Jump"))
         {
         Globals.Levels=0;
         Application.LoadLevel(Globals.Levels);
         }    
     }
 }
Comment

People who like this

0 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 28, 2014 at 09:07 PM 1
Share

I'm not a Javascript/Unityscript guru, so I'll leave this here as a comment (in case there is a better way). Change line 4 to:

  var lives:GUITexture[]=[null as GUITexture,null as GUITexture,null as GUITexture];//el numero de vidas
avatar image Sandman1980 · Mar 28, 2014 at 09:21 PM 0
Share

Thank you very much!!! it works!!!!!!!!!

avatar image Benproductions1 · Mar 29, 2014 at 10:40 AM 1
Share

Much simpler, you should have:

 var lives:GUITexture[] = [null, null, null] as GUITexture[];

Or even better:

 var lives:GUITexture[] = new GUITexture[3];
avatar image Sandman1980 · Mar 29, 2014 at 03:10 PM 0
Share

Thanks Benproductions!

1 Reply

  • Sort: 
avatar image

Answer by Benproductions1 · Mar 29, 2014 at 11:27 PM

Since null does not have a type, Unityscript can't infer the type of your array literal. To make Unityscript infer the type, you can explicitly state the type of null reference:

 var lives:Type[] = [null as Type];

For more simplicity, you can simply cast the entire stay to the type of array you need:

 var lives:Type[] = [null] as Type [];

But since you are just making a bunch of null reference, which are the default value for reference type arrays, you can simply make a new stay:

 var lives:Type[] = new Type[3];
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Movement.js(9,55): UCE0001: ';' expected. Insert a semicolon at the end. How could I fix this error? 0 Answers

Unet out of Sync Error 1 Answer

Problem with SAVE/LOAD, serializationStream supports seeking, but its length is 0 and Sharing violation on path 3 Answers

This error is killing me please help.GetComponent requires that the requested component 'MovementSettings' derives from MonoBehaviour or Component or is an interface. UnityEngine.GameObject.GetComponent 0 Answers

Unity.exe Application Error (0xc000007b) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges