• 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 seanwiemken · Apr 30, 2021 at 07:05 AM · prefabprefabs

Why is the value of my prefab variable being saved, and why is it different in the update and spawn method?,How to not save the value of a variable in a prefab object

Good day everyone,

I am having some trouble with the values of my variables being saved in my prefab and I do not want this. I am trying to make a turn-based tactics game and every time I play and exit the game one of my values does not change. This object is a prefab, but for some reason, it doesn't save the values of my variables on my other prefabs.

Here is my code:

 [SerializeField] GameObject basicRiflemanPrefab;
 [SerializeField] bool unitsBuiltThisTurn = false;
 
 // Start is called before the first frame update
 void Start()
 {
     this.unitsBuiltThisTurn = false;
 }

 // Update is called once per frame
 void Update()
 {
     Debug.Log("units built this turn:" + this.unitsBuiltThisTurn);
 }

 public void SpawnBasicRifleman()
 {
     Debug.Log("Units created: " + this.unitsBuiltThisTurn);
     Debug.Log("Selected ID: " + scrSelection.selected + " This ID: " + this.transform.gameObject.GetInstanceID());
     if (!this.unitsBuiltThisTurn && scrSelection.selected == transform.gameObject.GetInstanceID())
     {
         Instantiate(basicRiflemanPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z-3), new Quaternion(0, 0, 0, 0));
         this.unitsBuiltThisTurn = true;
     }
     /*else if (unitsBuiltThisTurn && scrSelection.selected == transform.gameObject.GetInstanceID())
         Debug.Log("This building cannot train anymore units this turn.");
     else
         Debug.Log("There is a different building selected: "+scrSelection.selected+" Units ID: "+this.transform.gameObject.GetInstanceID());*/
 }

When I look at the log that is called during the update method it shows it is false, however when I call the method for spawning the rifleman that log returns true. and I have no idea why. Even when I restart the game. I have tried making the variable private, public, and serializefield. Could it be calling the script on the prefab and not the object?

Comment
Add comment · Show 4
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 andrew-lukasik · Apr 30, 2021 at 07:08 AM 0
Share

Both [SerializeField attribute and public keyword make your field serialized. Remove both and it ceases to do that.

avatar image seanwiemken · Apr 30, 2021 at 08:40 AM 0
Share

It also does this with the private field which is what I had it as originally. After I missed with it a while longer I realized that all data is being pulled is from the prefab. For example when it creates the basic rifleman it is created based on the prefab transform not the actual objects transform

avatar image seanwiemken · Apr 30, 2021 at 09:54 AM 0
Share

Also, when I get the transform.gameObject.GetInstanceID() it's the id of the prefab, not the object.

avatar image logicandchaos · May 18, 2021 at 04:54 PM 0
Share

prefabs don't save values, you create a prefab and all instances are a copy of the original. If you want to save things between sessions you need to create a save load system, or could try playerprefs, not sure what you are trying to do tho..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by seanwiemken · May 18, 2021 at 04:41 PM

So I found out a little bit as to what is going on. But I am still unsure as to how to fix it. I am calling the method to spawn the rifleman from a button and to access the method I dragged the prefab of the barracks into the object slot for the button. So it seems that when I call the method it is calling the one that is attached to the button only. Does anyone have any ideas on how I can fix this? I have thought about using hashtables or ArrayLists, but I am not sure how I would create a reference to the object unless I use the instantiate function.

Comment
Add comment · Show 3 · 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 logicandchaos · May 18, 2021 at 04:57 PM 1
Share

To make a reference you must declare one as a variable in a script and to assign it you can drag and drop, find or other methods.

avatar image seanwiemken · May 18, 2021 at 06:36 PM 0
Share

Okay thanks, what do I need to make the find method work?

avatar image seanwiemken · May 18, 2021 at 09:41 PM 0
Share

@logicandchaos Thank you for recommending the find method! I was able to fix the issue by assigning the game object in a hashtable and comparing the id to that of the selected object. Then using the reference stored in the hashtable I could manipulate the correct data and get it to do what I needed it to.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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 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

New prefab system problem 1 Answer

Game Object is Instantiating on same position 2 Answers

What is the prefab is not valid since it has no root game object error? 1 Answer

some prefab collisions randomly stop working 2 Answers

Creating A Prefab Loses Hiearchy Connections 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