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 17, 2012 at 11:02 AM · arrayvariablestatic

Command all array variable values

I have a static array variable and i need to give a single command (from another script) to all the values in the array at once.

This is the situation, in Script1.js i have:

 static var value : float;
 
 value[0] = 0.1
 value[1] = 0.2
 value[2] = 0.3
 value[3] = 0.4

In Script2.js i need to code this, using a single command (otherwise it won't work):

 Script1.value[every value in the array] += 5;

Is it possible? Someone told me to put "id" inside the brackets (Script1.value[id] += 5), but Unity doesn't understand its meaning. I already tried to write:

 for (var i : int = 0;i < 3; i++);
 Script1.value[i] += 5;

But it doesn't work because all the 4 values become the same. What can i do?

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
1

Answer by Bunny83 · Jul 17, 2012 at 11:11 AM

First your variable isn't an array, it's just a float. Second you have to create the array with your desired size. Arrays can't change the size on the fly, if you need something like that, use a generic List.

 static var value : float[];  // this is an array
 
 
 // create the array with 4 elements (index 0 - 3)
 value = new float[4];
 value[1] = 0.1;
 value[2] = 0.2;
 value[3] = 0.3;
 value[4] = 0.4;
 
 // this will add 5 to each value
 for (var i = 0; i < 4; i++)
     Script1.value[i] += 5;

Btw, you have a semicolon after the for loop, so the body of the loop is empty. Your code effectively looks like this:

 for (var i : int = 0;i < 3; i++)
 {
 }
 Script1.value[i] += 5;

which doesn'T make much sense.

You also didn't even say what exactly you want to do. Try to rephrase your question.

Comment
Add comment · Show 2 · 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 17, 2012 at 11:46 AM 0
Share

I had already written the correct code, i had forgot to report it here. The variable declaration already was:

static var value : float[];

and before assigning the single values to every array value i had already written

value = new float[4];

but i had forget to insert the brackets in the code! thanks for remembering me to do this! Anyway, i've put the brackets now but the result remains the same: all the 4 array values become a single value.

I'll be more specific: i'm trying to make 4 objects move following 4 different "Ping Pong" trails. But the objects are at different positions in space. The 4 array values are their position on the x axis, generated with 4 different Random.Range(). I want them to start moving at the same time starting from their current position, so in Script1.js they are randomly instantiated in space and in script2.js i would like to use their different x positions to start moving following different trails.

Right now the 4 objects jump to a single x value and then start moving all together.

avatar image Bunny83 · Jul 17, 2012 at 02:34 PM 0
Share

It seems you've asked that question already two times:

http://answers.unity3d.com/questions/283498/relative-and-absolute-values-problem.html
http://answers.unity3d.com/questions/284058/mathfpingpong-strange-behaviour.html

The for loop you used above certainly doesn't end up with the same value in each variable. When it's executed the values will be 5.1, 5.2, 5.3, 5.4

Is there a reason why you want to handle all those objects form one script? Why not just put this script on each object? Place them where ever you want and they will pingpong between - amplitude and +amplitude around their starting position.

 var amplitude = 4.0;
 var speed = 1.0;
 
 private var startPos : Vector3;
 
 function Start()
 {
     startPos = transform.position;
 }
 
 function Update()
 {
     transform.position = startPos + Vector3($$anonymous$$athf.PingPong(Time.time*speed, amplitude*2.0) - amplitude, 0, 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

Static array variable 1 Answer

Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers

Storing many array values in a variable 1 Answer

Static variables 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