Unity Script does not update in Visual Studio - c#

I am new to using unity and also coding in C#, the problem I'm having is that when I change the value of a variable inside visual studio the code in the c# script does not update. I am not sure what the problem is I have tried a bunch of different methods but nothing seems to work. The current version of unity I'm using is 2020.1.17.f1 on mac os, and I am using visual
studio 2019. Below you can see an example of my code. In the unity console, the correct output isn't displayed. The variable currentAge is set to 17, but the output inside of Unity is 31.
I am not sure how to fix this issue, if anyone has any ideas I would greatly appreciate the feedback.

This is no "issue", just a misunderstanding ;)
The field
public int currentAge;
is serialized.
What this means in detail you can read in Script Serialization. In short this means this value is
visible/editable via the Inspector
saved together with the according asset (ScriptableObject/prefab/scene)
loaded during the initialization and deserialization process
What you can provide in your code is only a default value until you change it via the Inspector. As soon as you change that field value via the Inspector it will get overwritten with this now serialized value. So the next time you hit Play and this script instance is deserialized from the according asset file the value will be loaded and overwrites any default value you have set in your code.
And of course changing the serialized field value will NOT cause a recompilation of your script. You will not see the change reflected in your script! Which makes totally sense: What should happen if you have multiple instances of this script on different GameObjects all with different values? ;)
Also the other way round: If you change that value later in the code you only change the default value, the one in the Inspector, the serialized one will always overrule it.
There are some(?) IDEs (I only use Rider) which are capable of sniffing into the Unity references and asset values and can tell you with what value certain serialized fields are currently serialized/saved with but usually you can only go by what is either set in the Inspector or in the Unity messages like Awake, Start, OnValidate etc.

this is not bug orsomething, it is because you chnaged the value inside the Unity editor, that updates the value but it doesn't update the value inside Visual Studio

First of all, if you set a variable in the Inspector, this is a priority and will always remain the one in the inspector, only if you do not change it in Start or Awake.
To understand, if you changed the value of the variable to 31 in the inspector, then it will remain 31 even if you change in VS.

Related

UNITY C# - How can I save a Game Object, that has been instantiated in a runtime, in a runtime?

Ive got a Game Object that I instantiate in a runtime. Then, I make some changes to it like: I add some children objects, components etc.
I would like to somehow save this newly created object for me to use in other scenes and sessions.
Question is: how?
Thank you.
PS obviously I cannot use UnityEditor namespace
If you want to re-use the object in other scenes, you can use DontDestroyOnLoad method to prevent it from getting destroyed upon changing scenes. Afterwards, if you goal is to spawn copies of this object, you can disable it and treat it as a prefab and instantiate copies of it when necessary.
If you want to be able to re-use an object after re-launching the game, you might wanna check out ways of saving data in Unity. One simple way would be using PlayerPrefs but you would have to write the code for loading/saving the state of your object on your own since it's capable of directly saving only int, float, string data type values.
Here are a couple of references:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
EDIT:
A more specific solution you can try is serializing a custom class made by you to a JSON file. That class would contain the necessary data to re-create whatever GameObject you had in the scene.
Creating a prefab outside the editor isn't an option. Here's a reference: https://www.reddit.com/r/Unity3D/comments/a5c02u/save_gameobject_as_prefab_at_runtime/

Recovering inspector values from Unity build

I made the stupid mistake of moving a C# script out of the project and back in. This, of course, cleared all of the important inspector values back to their default values from the script.
All I need is to see those values again and re-enter them. I attempted decompiling the assembly-csharp.dll from my last build using dotPeak. While this did recover the correct classes and their fields, all of the fields are not defined. Where in a Unity build are these values stored, and is it possible to decompile them there?
Thanks in advance!
As far as I know there are stored in the scene file (.unity) or, if it's a prefab, in the prefab file (.prefab) (and if it's a prefab in the scene, it's stored in the prefab file with a list of modifications in the scene file). You might have success finding some values in there, but those are serialized and you can only really read them with Asset Serialization Mode "Force Text". It might also be that they lose their value when you open Unity between moving the script out and moving it back in.
Edit:
I missed the part that you wanted to read them from the build. I don't think that's possible, as they are normally serialized (if they are not in Streaming Assets). As scene files (which I think would contain the data) are also just files and not scripts that get compiled, I think the values are in one of the serialized files.
Also: You don't happen to have any kind of version control? Because then you could rollback to an old commit that contains that data, or if that's not possible look at the specific file in question.

Unity scriptable objects references lost using GIT

I'm currently working on a project where we use scritpable objects to store all configs on the game. The problem comes using git since sometimes we lose references stored on those SO.
For example I have a Config called A with a GameObject variable. On my branch "Branch1" the variable is null and on my branch "Branch2" is assigned to a prefab. I'm working on Branch1 and I move to Branch2 but the SO is not updated and the variable is still null although on Branch2 that variable actually had a value.
We have tried to reimport the asset but does not solve anything. The only thing that seems to work is a weird process. If we make a change to the SO after moving to BranchB, we save and then discard changes from git (we use sourcetree) the SO config now is fine as it should be on the current branch.
It's a weird behaviour and more weird solution we have that is leading to a lot of time lost and several errors.
Anyone has an idea of what is happening and knows how to solve it??
We are using Unity2019.1.14f1 and we use Odin for a custom inspector of the SO (maybe is relevant).
For anyone stumbling upon this question:
Save the project, under File->Save Project, and you will see your SO opening up for a commit in your preferred sourcecontrol software.
Kurt-Dekker (Thread linked below) goes into detail as to why just "Save as" and "Save" don't work, but "Save Project" does.
First answer by Kurt-Dekker in this thread: https://forum.unity.com/threads/scriptableobjects-are-not-updating-when-changed-on-the-filesystem-via-git.834109/

Can't add scripts - "Can't add script behaviour OVRAvatarLogger. The script needs to derive from MonoBehaviour!"

I've found a script and am trying to add the script to an empty game object in Unity3D.
From what I've read on stack & the documentation,
"The class name and file name must be the same to enable the script
component to be attached to a GameObject"
As far as I can tell, the script name, game object name and public class within the script file are all named "VRDraw" (see screenshot).
Does anyone know what I am doing wrong here?
(I've also tried deleting and re-creating the scripts/game objects with the proper name from the start, but this didn't work unfortunately)
Also, I can't find OVRAvatarLogger anywhere in the script. Does anyone know where this comes from?
you have another error in your script, fix all errors in your script and unity will recompile all files, so you can add your script to game object
If you notice the red letter on the bottom of the Unity app, you can see a compilation/syntax error, regarding the namespace "Enums".
This might prohibit the IDE from continuing to "work" on your script and attaching it, since it does not pass the syntax check.
Make sure you resolve the reference error first, and then try to re-compile and "play"

Build an AssetBundle runtime

I'm using Unity and I need a button that calls a function that allows me to build an AssetBundle, so I can make a software external from unity that allows me to build AssetBundle; is it possible?
Thank you all.
Build an AssetBundle runtime
This can not be done. bulding assetbundles requires the buildPipeline which is in the UnityEditor namespace which can not be accessed at runtime as it is editor only.
Making an external application for this that you call from within Unity would most likely also not work unless you know how to replicate their pipeline in your own application, and i doubt you will be able to call it form outside Unity (And even if you could it would still not work at runtime as assets will become read only)
I'm not sure why you would want to build assetbundles at runtime, but the only method i can think of that may work for this would use JSON/XML.
This would need two Instances of unity running to do it in "real time". The first instance of unity would be running your game with the object you want to make into anassetbundle. After clicking your button a function would be called that gets all the information on your GameObject (components, values, ID's, literally everything) and parse it into a XML/JSON file. This file will then be uploaded/saved somewhere where your second Unity instance can access it. The second instance of unity will then read this JSON/XML file in editor time, reconstruct the original GameObject from the data within (This can be done in editor time from script) and then put this reconstructed object through the assetbundle pipeline.
Do take note though that i've never done this myself, and am not 100% it will work. Though i am fairly confident if done correctly it should work.

Categories

Resources