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 /
  • Help Room /
avatar image
0
Question by shuvos3 · Dec 09, 2015 at 04:13 PM · prefab-instanceprefab changing at runtime

Changing prefab instances through script

I made a prefab gameObject with a rigidbody component attached. Whenever I change the mass of the original prefab from the inspector, all the instances present in the scene get affected (the field is not overridden). But when I try to do the same thing using a script, the mass of the instances remain unchanged (only the main prefab is affected and every time I enter play mode, it retains its previous value!). The script is attached to another gameObject and looks like this:

   public class CubeScript : MonoBehaviour {
     
         public GameObject largeCube; // dragged the original prefab in the inspector
         private Rigidbody rb;
         // Use this for initialization
         void Start () 
         {
     
             rb = (Rigidbody) largeCube.GetComponent ("Rigidbody");
             rb.mass = 44; // this is not changing the instances, rather only the main prefab. Note that the mass is not overridden
     
         }
     
     }

I don't understand as a beginner. The tutorials say that it is the same whether you change something from the inspector or using a script like that. Please explain this to me.

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

Answer by JedBeryll · Dec 09, 2015 at 04:43 PM

That is probably disabled at runtime on purpose to prevent unexpected behaviour. As you just dragged the prefab in the inspector, largeCube references the saved prefab not an actual object in the scene. If you want to change the instance's value then you need to have a reference to an instance in the scene. If you want to change all instance's values, you should use a list or an array to reference all of your instance's and iterate through them and change them one by one.

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 shuvos3 · Dec 10, 2015 at 10:31 AM 0
Share

That is probably disabled at runtime

That is correct. When I enter play mode and change the original prefab it doesn't change the instances when the game is running, but whenever play mode is exited the instances immediately get those value that are changed while the game was running! For script, why didn't the same happen?

avatar image JedBeryll shuvos3 · Dec 10, 2015 at 01:57 PM 1
Share

It doesn't happen because the editor changes the values. It is an editor feature not a runtime thing. The editor serializes the prefab when you change it and it knows it should change all instances too. At runtime, your scripts just do what they are told to do, and they don't check if the original prefab on the hard drive was changed or not.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity overwrites prefab values in runtime 0 Answers

Is there a way to control when lighting is generated? 0 Answers

Prefab Animator Attributes Not Updating 0 Answers

Help I've Lost all Hope, A script stopped working on my Player Character Prefab! 0 Answers

mouse click if (Input.GetMouseButton(0) 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