x


Unity 3D Newbie Quetion in javascript.

Trying to use classes inside scripts to display mutliple objects.

They seem to create successfully although only the last one displays on screen, using GUI Functios for this currently.

I'm stuck as to why i only see the last invader on screen and the others simply don't appear.

Did some testing with Debug.Log and they all seem to be crated okay. Seems to be a sticking block for me :(

////////////////////////////////////////////////////////////////////////////////

//Global declarations

static var MAX_ENEMIES = 40;
var tex_enemy:Texture2D;

 // SPRITE CLASS START

 class CSprite{
 var mytexture:Texture2D;
 var x =0.0f;
 var y= 0.0f;
 var xspeed = 0;
 var yspeed = 0;
 var alive =1;
 function CSprite(){
 }

     function Init(inx, iny, inalive){
     mytexture = Resources.Load("invader");
     x = inx;
     y = iny;
     alive = inalive;
     }

     function Display(){
        if (alive == 1){
        GUI.DrawTexture(Rect(x,y,8,8),mytexture);
        }
     }

 }
// SPRITE CLASS END

static var enemies = new Array();

////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////

//Inside Start

tex_enemy=Resources.Load("invader");
var tempenemy = new CSprite();
tempenemy.Init(100,100,1);
tempenemy.mytexture = tex_enemy;
    for (i=0; i<MAX_ENEMIES; i++){
    enemies.push(tempenemy);
    enemies[i].Init((i*16), 100, 1);
    enemies[i].mytexture = tex_enemy;
    }
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////

//Inside ONGui

    for (i=0; i<MAX_ENEMIES; i++){
    enemies[i].Display();
    }

////////////////////////////////////////////////////////////////////////////////    
more ▼

asked Jun 26 '11 at 03:29 PM

refreshcreations gravatar image

refreshcreations
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

You just created one instance of your CSprite class but pushed it several times in the array. That's why all elements points to the same sprite. That's why only the last set values are used.

for (i=0; i<MAX_ENEMIES; i++){
    var tempenemy = new CSprite();
    tempenemy.Init((i*16), 100, 1);
    tempenemy.mytexture = tex_enemy; // You set the texture already in Init so i don't get why you do this here again.
    enemies.push(tempenemy);
}
more ▼

answered Jun 26 '11 at 03:57 PM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

(comments are locked)
10|3000 characters needed characters left

Thanks very much :) That's fixed it up!

more ▼

answered Jun 26 '11 at 04:34 PM

refreshcreations gravatar image

refreshcreations
1 1 1 2

Don't post comments as answers please.

Jun 26 '11 at 05:36 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x337
x293

asked: Jun 26 '11 at 03:29 PM

Seen: 568 times

Last Updated: Jun 26 '11 at 05:36 PM