Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment · 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

$$anonymous$$uch 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

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

error CS1061 and error CS0117 scripting InputField 1 Answer

Member cannot be accessed with an instance reference; qualify it with a type name instead 1 Answer

Getting this error every time i load my project (both 5.6.0f3 and 5.5.0f3, anyone can guide me to fix it? 0 Answers

Help with basic AI script 1 Answer

How can I baked Lighting with a large scene? 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