Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Monko · Jul 04, 2013 at 02:59 AM · another scriptchange variable

Changing a variable in another script for one object.

Ok, I already know how to change the variable in another script. All you do is this:

 GameObject.GetComponent(ScriptName).variableName +=1;

But my problem is I have to change the variable TestVariable located in one Gameobject, even though the script is in many gameobjects.

That Probably didn't make sense, so let me explain.

I have hundreds of game objects stored in a 3-dimensional array called "chunks", like this:

 static internal var chunks : GameObject[,,] = new GameObject[5,5,5];
 for(var z=0;z<chunktotal;z+=1) {
 for(var y=0;y<chunktotal;y+=1) {
 for(var a=0;a<chunktotal;a+=1) {
 chunks[x,y,z]=GameObject.Instantiate(chunkObject,Vector3(x,y,z),Quaternion.identity);

This creates 125 of the object "chunkObject". "chunkObject" is a GameObject that has the component "ScriptA" in it. So now lets say I want to change a variable in the script of of just one of the 125 gameobjects I created. Here is how Im currently doing it:

 chunks[0,0,0].GetComponent(ScriptA).testvar =1;

This code works. Except for one problem. Every single testvar in the objects stored in chunks[x,y,z] is changed. For example, running the code above would change all 125 gameobjects have testvar set to 1. Even though I specified which one I wanted to change! (In this case, chunks[0,0,0])

So does anyone know how to edit this code to change testvar of just one gameobject, not all 125?

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
0
Best Answer

Answer by ebogguess · Jul 04, 2013 at 03:35 AM

I can't see the code that defines testvar, but I would wager that you've defined it as static. When you change a static variable, any instances that can see that variable will be updated as well.

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 Monko · Jul 04, 2013 at 04:25 AM 0
Share

Yup, testvar is in fact static. I finally understand why this was happening!

now that I know how static variables work, Im going to edit my project further to see if I can solve it. ill post any problems or solutions I find. Until then...

avatar image Monko · Jul 04, 2013 at 12:25 PM 0
Share

Finished!!!!! makeing testvar a static variables was the only problem. All I had to do was take it away, make testvar non-static.

avatar image
1

Answer by JiffyJuff · Jul 04, 2013 at 03:09 AM

You make a boolean. Then you set this variable of the target object to true in the inspector. Then you test if it is true. If so, chang testvar to 1.

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 Monko · Jul 04, 2013 at 04:15 AM 0
Share

unfortunatly, The inspecter is out of the option because I create the objects during gameplay.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

enable/disable script from another script c#? 2 Answers

Calling a function in another script. 1 Answer

change variable in script attached to parent using triggers 1 Answer

Random Compiler error 1 Answer

default script editor now working anymore 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