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 rhbrr5hrfgdfgw · Jan 19, 2015 at 07:11 PM · javascriptgameobjectvariablerandom

GameObject variable change for all of the objects

I know this is much to read but thanks for any help

There is 2-5 instantiated prefabs, they all have the same script named 'walk' and they all have a variable named stage, i made a for loop to respawn the prefabs(just a random number every time). in the loop i have another random variable called randomX which responsible for the x position in the screen of the prefab. anyways if the its spawns on the left side of the screen i want the prefab to move to the right side by changing the stage variable(string) to "ltr" and if its spawns on the right side of the screen i want the prefab to move to the left side by changing it to "rtl",this works but the problem is that its changing for all of the prefabs and not just to to the current prefab.

Spawn script:

 for(var k : int = 0; k < respawnNum; k++){
                 var yRandomPos : float = Random.Range(6.98,-4.8);
                 xRandomPos = Random.Range(14.0, -14.0);
                 var position2: Vector3 = Vector3(xRandomPos, yRandomPos, -3.980108);
                 var pref2 : GameObject = Instantiate(prefab, position2, Quaternion.identity);
     
                 if(xRandomPos < 0){
                     pref2.GetComponent(walk).state = "ltr";
                 }
                 else if(xRandomPos >= 0){
                     pref2.GetComponent(walk).state = "rtl";
                 }

Walk script:(every instantiated prefab has this script)

     function Update(){
 if(this.state == "ltr")
         transform.position.x += finalSpeed * Time.deltaTime;
     else if(this.state == "rtl") transform.position.x -= finalSpeed * Time.deltaTime;
 }

finalSpeed is just a variable.

Comment
Add comment · Show 2
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 · Jan 19, 2015 at 07:22 PM 0
Share

Are you using a static variable? If so, don't. (Also, you should really use an enum for the state, rather than using strings, which are more prone to user error and won't generate compiler errors if you mess them up.)

avatar image rhbrr5hrfgdfgw · Jan 19, 2015 at 07:31 PM 0
Share

I dont have any errors, and yes i do use static variable to access it from other script, and i dont really know how to use enum good. if you can explain to me and also if you can to show me with my code. again if you didn't understood my problem: the variable changes for all the gameObject who have the script and not just to the current gameObject

1 Reply

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

Answer by NoseKills · Jan 19, 2015 at 08:56 PM

and yes i do use static variable to access it from other script

A variable only has to be declared "public" to be accessible from other classes, not "static". "Static" means the variable is defined for that class, not independently to each instance of the class. So if "state" is declared static, it does change everywhere no matter where you change it.

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 rhbrr5hrfgdfgw · Jan 19, 2015 at 09:01 PM 0
Share

Ok, i didn't really knew this so thank you very much

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

How would I be able to subtract from a variable when it hits something? (OnCollisionEnter) 1 Answer

Acces to a var from another Script. 2 Answers

Lottery system 1 Answer

How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers

Scripts accessing one another (JS) 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