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 Dreamhaxor · Jan 19, 2014 at 02:41 PM · editorprefabruntimeinstancebool

changing a bool in a prefab doesn't change in it's instance.

My problem is that the a instance of a prefab seemed to perfectly inherit everything form the prefab, until i introduced a public bool. the thing is when the bool is changed in the prefab during runtime, it doesn't change in the instance, but if changed in editor it does change in the instance. The actual mechanic is: when a bullet shoots the player the bool "lose" in the camera prefab is changed to true. the lose is changed to true in the prefab, but doesn't change in the instance of the camera.

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

2 Replies

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

Answer by Hoeloe · Jan 19, 2014 at 03:19 PM

A prefab is only a compile-time construction. You shouldn't really mess with it at runtime without very good reason. The reason it doesn't work is that there isn't really a connection between the instances and the prefab - that's just an abstraction for the editor. Once an object is created, it's an object, not connected to the prefab, so by changing the prefab, you change only things instantiated AFTER the change.

Now, as for how to fix it, what you're looking for is a static variable. Normal variables are instance-based, that is, there's a new version of a variable for each instance. Let's try an example. Take this class definition:

 public class Test
 {
     public int a;
     public static int b;
 }

Now, if we do this:

 Test one = new Test();
 Test two = new Test();
 
 one.a = 4;
 two.a = 5;
 Test.b = 6;

We find that they each contain these values:

 one:
 a = 4
 b = 6
 
 two:
 a = 5
 b = 6

Note that the second variable is the same in both, and that we assigned it by referencing the class rather than an instance. This is because it is a static variable - it it class-based, rather than instance-based. There is only ever one copy of a static variable, and it is shared between all instances of the class. This, I think, is what you're trying to achieve, and this is a much better way to do it.

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 Dreamhaxor · Jan 19, 2014 at 04:07 PM 0
Share

thx. i somehow forgot that static variables exits lol..

avatar image
0

Answer by Dreamhaxor · Jan 19, 2014 at 03:18 PM

Nvm found the answer my self. i just did Cam=GameObject.FindGameObjectWithTag("MainCamera") and Cam.GetComponent<SomeScript>().lose=true to actually change the bool on the camera it self instead of the prefab. But none the less it still bugs me that instances don't inherit form Prefabs during runtime...

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 Hoeloe · Jan 19, 2014 at 03:30 PM 0
Share

Worth looking at my answer - it might help explain why changing the prefab didn't work.

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

19 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

Related Questions

How to run script in editor 1 Answer

Changing a prefab's name (via the editor) doesn't apply the new name to the prefab's instances? 1 Answer

How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer

What is runtime analogy for Editor's PrefabUtility.SetPropertyModifications? 0 Answers

Saving a spheremodel into a prefab 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