Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 refreshcreations · Jun 26, 2011 at 03:29 PM · arraybeginnerclass

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();
     }
 
 ////////////////////////////////////////////////////////////////////////////////    
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 26, 2011 at 03:57 PM

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);
 }
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
avatar image
0

Answer by refreshcreations · Jun 26, 2011 at 04:34 PM

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

Comment
Add comment · Show 1 · 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
avatar image Eric5h5 · Jun 26, 2011 at 05:36 PM 1
Share

Don't post comments as answers please.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Storing Data and Access Efficiency 0 Answers

2d array.What is the difference between these two? 1 Answer

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

How to create Random Stats matching the weapons? 0 Answers

Convert class of int vars into an array 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