Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Can't save initial values of public text variables

Discussion in 'General Discussion' started by tvisarl, Dec 3, 2023.

  1. tvisarl

    tvisarl

    Joined:
    Aug 26, 2013
    Posts:
    22
    Hi, all!
    Here's my problem: I'm creating a game that has an armored car (purchased from the asset store) in it. I add to this car a script component called WheelDriver1. As you can see from TV01.jpg I have created public variables in this script : three (topmost) for the UI and others for the wheels and their colliders.
    And everything works fine when these variables are set up (TV03.jpg). But... if I try to save the armored car that runs so well as a new asset, the GUI variables' initialization texts are not saved and I must set them up again.

    Crazy. Won't do. Can you help?
    Thanks in advance.

    Thierry

    PS: There are no overrides for the asset. TV01.jpg TV03.jpg
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,574
    What do you mean by "save the armored car as a new asset"? Do you mean a prefab?
     
  3. tvisarl

    tvisarl

    Joined:
    Aug 26, 2013
    Posts:
    22
    Yes. Excuse me, I should have been more precise. And this what I tried to do.
    TV04.jpg
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,574
    Ok, I'm not sure exactly what your exact scenario is here, but there are two common prefab stumbling blocks that I see people run in to:

    1. When you save a prefab, it does not save any references to other objects or components in the scene (unless those are also in the same prefab). That's because the prefab itself is not in any scene, so having references to other objects in a scene does not make sense.
    That means if you have a car and one of the components has a reference to a GUI object in the scene, unless your GUI is a child of that car or something, then your car prefab will lose any references to the GUI object.
    You can always make an empty game object at 0,0,0 and make both your car and GUI a child of the empty game object. Then when you make a prefab of that empty object, it will include both the car and the GUI and any connection between the two.

    2. If you've made updates to the car object in your scene, that does not automatically update the corresponding prefab. If you want the prefab updated, you have to save your "overrides".

    https://docs.unity3d.com/Manual/PrefabInstanceOverrides.html
     
    Ryiah likes this.
  5. tvisarl

    tvisarl

    Joined:
    Aug 26, 2013
    Posts:
    22
    Oh, I see. Your answer is very useful, thanks a lot :).