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 CrazyIvanTR · Oct 08, 2016 at 02:49 PM · c#unity 5scripting problemunity5scriptingproblem

Script sets same value to other script in all objects instead of just one.

I'm adding a script component to an object in a loop like so:

  {
  clone = (GameObject)Instantiate(hexagon, new Vector3(x,y,z), transform.rotation);
  clone.transform.parent = gameObject.transform;
  clone.name = "gridTile:" + j + "," + i;
  clone.AddComponent<Properties>();
  }

Then later on in the code I'm looking for the object that was created (among many) and then setting a value in the Properties (again with a loop so that all objects have values in them) like so (edited to put more code that showed what I did wrong, should've made the new string array in the loop itself, thanks @Bonfire-Boy for the comments):

 string[] conTemp = new string[6];
 for loop{
 clone = GameObject.Find("gridTile:" + j + "," + i);
 {
 string[] conTemp = {some numbers are set during code};
 }
 clone.GetComponent<Properties>().connections=conTemp;
 }

Now the problem is, the code goes through them all and just sets them all the same value instead of different values. If I just run it out of the loop, it sets the right values but the loop somehow breaks it. For example the first one should have something like 1,2,3,4,5,6 as values but the second object should have 2,3,4,5,6,7 and instead they both get 2,3,4,5,6,7.

This is the only thing in the properties script:

 public class Properties : MonoBehaviour {
 public string[] connections = new string[6];
 }

The whole code is uploaded as requested. As it is a messy work in progress code which is really long, I didn't want to put it in the question itself and couldn't figure out how to make a spoiler tag if it's possible, so here it is as an attachment : link text

hexagonalhexgenerator.txt (6.8 kB)
Comment
Add comment · Show 3
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 Sergio7888 · Oct 08, 2016 at 03:47 PM 0
Share

Need the loop codes or methods code to help, codes fragment you provided are too vague.

avatar image Bonfire-Boy · Oct 08, 2016 at 05:09 PM 0
Share

I think there's a problem with this code...

  clone = GameObject.Find("gridTile:" + j + "," + i);
  {
  string[] conTemp = {some numbers are set during code};
  }
  clone.GetComponent<Properties>().connections=conTemp;

The conTemp variable declared within the inner code block is local to that block. You're setting the connections variable to a different conTemp variable, which you're not modifying.

That's actually the only effect of those curly brackets. Is there a reason you've put them there and have you tried just removing them?

avatar image CrazyIvanTR Bonfire-Boy · Oct 08, 2016 at 06:18 PM 0
Share

That's a block of code for ease of access (so I can keep track of what I wrote), it's actually in the same namespace as the other codes.

So those brackets are actually not doing anything (other than notation), and it does actually work, the code does the work and it sets the conTemp into the connections but my problem is, it doesn't just set it on one object, it just sets it on every object in the scene that I have set the connections beforehand (if I set the connections on one properties script then go on to the next one to set it, the previous one also gets set the same values).

1 Reply

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

Answer by Bonfire-Boy · Oct 08, 2016 at 06:34 PM

Ok, I can see now that you've added the full code as an attachment. Sorry for not having spotted that when I posted my comment, above. Incidentally, this does demonstrate the importance of posting your actual code and not oversimplifying it for a question, because the issue in the full code is completely different (practically opposite to that in the simplified code).

The problem in the full code is that you are reusing the conTemp array and not creating it in the place shown in the simplified code!

You're looping through your tiles, (re-)populating that array and then setting the tile's connections variable to point to the one-and-only conTemp array. Like I say, this does not create a new array, it just makes that tile's connections variable point to conTemp. So, naturally, they all end up pointing to the same thing.

Instead, you actually want to do what the code in the question does, and create a new array each time through the loop.

So I think all you need to do is move the declaration of conTemp from outside the loop to inside the loop, so that the real code looks more like the simplified code (but without those pointless curly brackets).

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 CrazyIvanTR · Oct 08, 2016 at 07:00 PM 0
Share

Wow, that could be it. I'll look into it and let you know if you should put this as the answer :)

Alright, yep that did it. I'll change the simplified code to include the wrong version and note at the bottom what the change was. You should just copy and paste this as an answer so I can select it.

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

7 People are following this question.

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

Related Questions

where have i went wrong in my script (unity 5) 0 Answers

Refresh panel with prefab contained value from json that created using array 0 Answers

Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers

Manipulating the location of the VR controllers (Vive/SteamVR) in script 0 Answers

Using AirBrakes on mobile device with Standard Assets AircraftController 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