- Home /
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);
}
}
}
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
$$anonymous$$uch simpler, you should have:
var lives:GUITexture[] = [null, null, null] as GUITexture[];
Or even better:
var lives:GUITexture[] = new GUITexture[3];
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];
Your answer
![](https://koobas.hobune.stream/wayback/20220613141429im_/https://answers.unity.com/themes/thub/images/avi.jpg)