Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Aarorie · Feb 21, 2017 at 12:29 PM · prefab changing at runtime

Make similar objects the same prefab?

So i'm making a survival game and I randomly generated trees using Random.Range and Instantiate(), and it was done during playmode. So my question is, is there anyway to make the 1500 trees in my game all the same tree prefab, so if i change something on the prefab it will change it for all the trees?

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 NoseKills · Feb 21, 2017 at 04:47 PM 0
Share

How are you creating trees now? That's pretty vital information.

Instantiating from a prefab does exactly what you describe (if i understood correctly)

avatar image Aarorie NoseKills · Feb 21, 2017 at 05:07 PM 0
Share

pragma strict

var tree : GameObject; var AmountTrees : int;

function Start() { InvokeRepeating("GenerateTrees", 0, 0.0001); }

function Update () { if (AmountTrees <= 0) { CancelInvoke("GenerateTrees"); } }

function GenerateTrees() { AmountTrees--; Instantiate(tree, Vector3(Random.Range(54.61, 419.4), 100, Random.Range(57.24, 437.3)), Quaternion.Euler(0,0,0)); }

avatar image Aarorie Aarorie · Feb 21, 2017 at 05:07 PM 0
Share

The names appear white, so is there anyway to make them blue? And they're all attached to the same prefab

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Feb 21, 2017 at 06:35 PM

Don't use "Instantiate". Instantiate will just create a copy / clone of the source object. The "prefab link" is a pure editor feature. To preserve that link you have to use the editor method PrefabUtility.InstantiatePrefab inside an editor script. Again the concept of a prefab connection doesn't exist at runtime. Prefabs at runtime are just off-scene objects that can be cloned into the scene.

For this it might be a good idea to create a ScriptableWizard where you can select your prefab and set all the parameters (number of trees) and then generate them inside OnWizardCreate.

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 Reedex · Jan 14 at 11:38 AM 0
Share

Thanks, you rock. In case anyone wants this :d. Delete the old ones on your own rather. using UnityEngine; using UnityEditor;

 [ExecuteInEditMode]
 public class ObejctSwapper : MonoBehaviour {
 
     public GameObject Prefab;
 
     public GameObject[] ObjectsToExchange;
 
     public bool SwapNow;
 
     void Update () {
         if (SwapNow) {
             for (int i = 0; i < ObjectsToExchange.Length; i++) {
                 
                 GameObject newObject = (GameObject) PrefabUtility.InstantiatePrefab (Prefab);
                 //newObject.transform.parent = ObjectsToExchange [i].transform.parent;
                 newObject.transform.position = ObjectsToExchange [i].transform.position;
                 newObject.transform.rotation = ObjectsToExchange [i].transform.rotation;
                 newObject.transform.localScale = ObjectsToExchange [i].transform.localScale;
             }
             SwapNow = false;
         }
     }
 }

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

Arkanoid clone: How can I enable/disable colliders in my prefabs while the game runs? 1 Answer

Highlighting prefabs with different colors when a first person player looks at them,Highlighting objects programatically with different materials when the first person player looks at them 0 Answers

How can I instantiate a button prefab, complete with onClick() method, at runtime? 1 Answer

How to attach a Script/GameObject in the Scene view to a Prefab in the Assets folder. 1 Answer

Changing the variable 'power' of ONE turret instead of ALL turrets 1 Answer


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