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 aLyon · Jul 11, 2010 at 12:43 AM · javascriptinstantiatearray

Array variable assigned to instantiated object changes in all instantiated objects?

Hi, I'm having an issue with setting up variables in an instantiated object. It appears that when I try to setup an Array variable in the instatiated object, it works the first time, but when I create anymore of the same object, the array variable is updated in all of them.

Whats frustrating though is that other variable types (i.e int, float etc) stay unique to the instance.

I came across this question, which suggests the problem might be that I'm changing the variables inside the prefab itself and not the instantiated object, but it doesn't seem to work with arrays.

Here is the relevent code: (with variable and function names changed for convienence)

MainController.js: Added to an empty GameObject in scene

var prefabToInstantiate : InstanceControlScript; private var array1 = Array(arrayLength); private var array2 = Array(arrayLength); private var someOtherVar : int;

//Some code which sets data in arrays and calls CreateInstance() when Fire1 is pressed

function CreateInstance(){ var newInstance = Instantiate(prefabToInstantiate, pos, rot); newInstance.arrayX = array1; newInstance.arrayY = array2; newInstance.someInteger = someOtherVar; }

I drag the prefab with the script below onto the prefabToInstaniate field in the inspector

InstanceControlScript.js:

private var arrayX : Array; private var arrayY : Array; private var someInteger : int;

function Update(){ //some code which reads and acts to the new arrays and variables }

Any help will be greatly appreciated!

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 Peter G · Jul 11, 2010 at 01:14 AM 0
Share

Not really an answer, but have you tried using .Net arrays?

avatar image aLyon · Jul 11, 2010 at 11:35 AM 0
Share

Yeah, tried that but no luck.

1 Reply

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

Answer by Eric5h5 · Jul 11, 2010 at 02:07 AM

Arrays are by reference, not value. Assigning an array to a variable merely creates another reference, it doesn't make a new copy.

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 aLyon · Jul 11, 2010 at 10:48 AM 0
Share

Ah, okay. How would I make a copy then?

avatar image Eric5h5 · Jul 11, 2010 at 04:54 PM 0
Share

@aLyon: $$anonymous$$ake a new array and copy all the elements from the original array. If you use an ArrayList ins$$anonymous$$d of Array then there's a Clone() method.

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

No one has followed this question yet.

Related Questions

How can I instantiate a GameObject directly into an array? 3 Answers

Instantiating from an object that's in an array 1 Answer

Instantiate into array : Out of range? 1 Answer

Instantiate object in for loop with array 2 Answers

Applying different textures to spawned objects. 2 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