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 neil.richardson · Jan 23, 2013 at 12:09 AM · prefabsserializableserializefield

GameObjects created from prefabs - members reverting on play.

Hello, I've set up a prefab and scattered a few instances of it around a level - but changed some of the members per instance. When starting the game, all members revert back to what were set on the prefab. Has anyone encountered this before? The values are changed before the first frame of started (verified by breaking in Start).

To give an idea of the setup, there is a VectorFieldAffector component (which is on the gameobject I am making the prefab for), which contains a VectorFieldForce. VectorFieldForce contains the members - all marked with [SerializeField], and the class as [Serializable].

Thanks!

Comment
Add comment · Show 6
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 superpig ♦♦ · Jan 23, 2013 at 12:18 PM 0
Share

Can you show us the code for your definition of VectorFieldForce?

edit: and also the definition of the VectorFieldForce instance variable

avatar image neil.richardson · Jan 23, 2013 at 12:22 PM 0
Share

Can do, will post it up in the morning, but from the top of my head it's basically:

[Serializable] public class VectorFieldForce { [SerializeField] public Vector3 Position;

[SerializeField] public Vector3 Force;

[SerializeField] public float AttnC;

[SerializeField] public float AttnL;

[SerializeField] public float AttnQ; }

avatar image superpig ♦♦ · Jan 23, 2013 at 12:24 PM 0
Share

The [SerializeField] attributes are actually redundant - public members of a [Serializable] class are all automatically serialized, provided they're of a serializable type themselves.

That looks O$$anonymous$$ though. Need the instance variable next...

avatar image superpig ♦♦ · Jan 23, 2013 at 12:30 PM 0
Share

Also, do you have field initialisers in your VectorFieldForce class (e.g. public float AttnL = 10.0f;) that happen to match the prefab values?

avatar image neil.richardson · Jan 23, 2013 at 12:30 PM 0
Share

Which is as follows:

[SerializeField] public VectorFieldForce Node;

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ryba · Jan 23, 2013 at 12:25 PM

You must set those values in inspector before even hitting play button. Every change while game is running is reverted to state of saved scene while you stop game.

[EDIT] Can you provide whole class code ? Are u setting those values anywhere in the code ? Are u accessing those fields in the code directly or through property / setter getter ? (you should use setters and getters even if your field is public for inspector) If you do so, then add debug log in setter, to check if setter resets your values, or if thats something else. If you are accessing fields directly, then consider refactoring.

Comment
Add comment · Show 5 · 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 neil.richardson · Jan 23, 2013 at 12:28 PM 0
Share

The values are changed in the inspector without the game being started, it's when I hit start they are reverting to the values that are defined in the prefab.

avatar image ryba · Jan 23, 2013 at 12:33 PM 0
Share

check out what i wrote after [EDIT] in my answer

avatar image neil.richardson · Jan 23, 2013 at 12:39 PM 0
Share

Only place the values are being set in code is in the editor class (which calculates the attenuation values and sets them). I can post up more in the morning when I'm back in the office.

avatar image superpig ♦♦ · Jan 23, 2013 at 12:40 PM 0
Share

Ah, the SetDirty call that people have been suggesting on Twitter sounds like a good thing to check then.

avatar image neil.richardson · Jan 24, 2013 at 12:22 AM 0
Share

SetDirty done the trick. Nice and easy :)

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

11 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

Related Questions

Can a GameObject be serialized? 1 Answer

SerializeField does not work when a value set using code 0 Answers

Custom Inspector for an array of a serialized class 0 Answers

How to prevent Editor script variables resetting at runtime 2 Answers

Starting Play mode resets values for serialized class in the Inspector 0 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