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 Mz3D · Jul 13, 2012 at 12:19 PM · arrayvariablestatic

Static array variable

Hello everyone, i need 3 instances of an object at 3 different positions on the x axis between x = - 4 and x = 4. Then in another script, i need these 4 objects to start a ping pong movement along the x axis. So it seems i need a static array variable. I created this code but unity gives me lots of errors. Can you help me?

Script 1 (named GameController.js)

 static var xpos : float[];
 
 function Start(){
  xpos = Random.Range (-4.0, 4.0);
     }
 
 function LevelEnd(){
     Instantiate(enemy, Vector3(xpos[0], 0, 0), transform.rotation);
     Instantiate(enemy, Vector3(xpos[1], 0, 0), transform.rotation);
     Instantiate(enemy, Vector3(xpos[2], 0, 0), transform.rotation);
 }

Script 2 (named EnemyMovement.js)

 var pingpongspeed: float;
 
 function Update () {    
     GameController.xpos += Time.deltaTime*pingpongspeed;    
     transform.position.x = Mathf.PingPong (GameController.xpos, 8.0);    
 }







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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Jul 13, 2012 at 01:24 PM

You first created a static variable named xpos of type float[], but then try to read it as a float (not an array).

You may need to change your script this way (it is just a suggestion):

Script 1 (named GameController.js)

 function Start(){
     xpos = new float[3]; // C#-syntax, not sure if this is the same syntax in UnityScript.
     xpos[0] = Random.Range (-4.0, 4.0);
     xpos[1] = Random.Range (-4.0, 4.0);
     xpos[2] = Random.Range (-4.0, 4.0);
 }

Script 2 (named EnemyMovement.js)

 var pingpongspeed: float;
 var id: int; // id is between 0 and 3 included, different for each instance
 
 function Update () {    
     GameController.xpos[id] += Time.deltaTime*pingpongspeed;    
     transform.position.x = Mathf.PingPong (GameController.xpos[id], 8.0);    
 }
Comment
Add comment · Show 4 · 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 Mz3D · Jul 13, 2012 at 02:05 PM 0
Share

it worked in a strange way, the istances teleport at the same position on the x axis and then start moving (following the same path).

avatar image Kryptos · Jul 13, 2012 at 02:16 PM 0
Share

This line is responsible for it (and I$$anonymous$$HO it does not make sense):

 GameController.xpos += Time.deltaTime*pingpongspeed; // or GameController.xpos[id] in my answer

I just pointed out the modification needed for the script to run. But you may have to review the logic.

The first parameter of $$anonymous$$ath.PingPong should be a time not a position.

avatar image Mz3D · Jul 13, 2012 at 02:56 PM 0
Share

i tried to change the code as this but the result is the same

function Update () {

transform.position.x = $$anonymous$$athf.PingPong (Time.time, 8.0);

}

avatar image Mz3D · Jul 13, 2012 at 03:04 PM 0
Share

i tried to change the code as this but the result is the same

 function Update () {
 transform.position.x = $$anonymous$$athf.PingPong (Time.time, 8.0) - 4.0;
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Script effects all gameobjects. 1 Answer

Command all array variable values 1 Answer

Increasing a score value? 2 Answers

Will static variables not work if negative? 1 Answer

Reset a static variable? 1 Answer


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