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 sanja15513 · Jan 03, 2019 at 03:17 PM · instantiateprefabs

Replace instantiated objects in an array

I have an array of instantiated objects. Now as I have an in game shop I would like to replace those objects with another prefab that was selected in shop. Is it possible to just assign a different prefab to already instantiated objects from an array. Or is it possible to instantiate empty game objects and then just assign different prefabs later?

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
0

Answer by Jebulan · Jan 03, 2019 at 06:12 PM

If I understand you question correctly: When you instantiate a new object, it returns a reference to that new gameObject. array[x] = Instantiate(name,options); Replacing an item in an array should be similar to how you originally populated it.

However I don't believe Unity will allow you to apply (or change) a prefab to an existing gameObject. Mapping variables between different prefabs that may have different scripts attached is problematic.

You can create empty gameObjects and instantiate other gameObjects inside of it, which is may be what you are after?

Comment
Add comment · Show 4 · 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 sanja15513 · Jan 03, 2019 at 06:48 PM 0
Share

At the beginning of the game I have an array of gameObjects that will be used in the game. Player can buy different objects from the shop, so what I want to do is replace those old objects in that array with the new bought one.

avatar image Jebulan · Jan 03, 2019 at 07:45 PM 0
Share

Assu$$anonymous$$g the array of gameObjects references existing objects in the scene: In code you will need to instantiate a new object and replace the old object with the new object in the array.

 newObject = Instantiate(name,options);
 oldObject = array[x];
 (You may need to match location,rotation.)
 newObject.transform.position = oldObject.transform.position
 array[x] = newObject;
 destroy(oldObject);

Even though we replaced the object in the array, the old object will still exist in the scene and will need to be destroyed.

Ultimately the scene is where the gameObject is stored, the array is just a reference to it.

Perhaps there's a better way, but afaik this would be the way to do it.

avatar image sanja15513 Jebulan · Jan 03, 2019 at 08:15 PM 0
Share

I was kind of looking for a way to not destroy objects all the time, just replace them..

avatar image Jebulan sanja15513 · Jan 03, 2019 at 08:40 PM 0
Share

Wasn't sure how your project is setup. Lot's of ways to approach this.

If they don't have visible objects, there's no need to destroy them other than cleaning things up. And you don't have to match position either, unless it affects game play.

$$anonymous$$y first reply should work in that case. (Instantiate the object into the array slot).

You could also have the objects in the shop already created in the scene, and in that case you could just swap array references. myobjects[x] = shopObjects[y]

The problem lies in trying to apply a prefab to an instantiated object. Which I don't think can be done.

Hope this helps

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

122 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers

How prefab the instance works? 1 Answer

Clicker game instantiate prefab depending on gold per click 0 Answers

Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers

instantiating a projectile continually over time 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