DontDestroyOnLoad not compiling in Visual Studio
Hello, I'm a beginner making a simple project using unity 2018 with Visual Studio as an editor. I've only just set it up and I tried to call the DontDestroyOnLoad on a simple script inside the Start() function.
The call is written as follows:
public class CBattle : MonoBehaviour { // Start is called before the first frame update void Start() { DontDestroyOnLoad.(gameObject); SceneManager.sceneLoaded += OnSceneLoaded; this.gameObject.SetActive(false); } ...
Visual Studio throws a warning and doesn't allow it to compile, even though I've seen another project use this method in this way. The warning text reads: "'Object.DontDestroyOnLoad(Object)' is a method, which is not valid in the given context".
This is strange, since calling a method inside Awake, Start or Update is something I thought was normal. Additionally, other errors I'm getting include certain references not being made automatic. If I call something in the Scene Manager, I have to include it in the script's references.
Could this be related to the issue? I appreciate your help.
Answer by juliacs · Apr 17, 2019 at 03:18 AM
Your DontDestroyOnLoad.(gameObject);
should be DontDestroyOnLoad(this.gameObject);
I have this method inside Awake and it works perfectly.
Answer by FBalerio · Apr 17, 2019 at 03:29 PM
I know it should. Both usages of DontDestroyOnLoad(), either with (gameObject) or (this.gameObject) throw the same warning inside both Awake and Update.
Notice you incorrectly used a dot. To be used as a method, as the warning says, you need to use DontDestroyOnLoad(Object)
ins$$anonymous$$d of DontDestroyOnLoad.(Object)
, without the dot.
Syntax was correct.
I solved this by cutting and pasting the code line into another part, then pasting it again in the same function.
It was a Visual Studio hiccup.
Your answer
Follow this Question
Related Questions
Unable to convert classes into dex format 1 Answer
error CS0118 'charanim.CameraTurn' is a field but a 'type' was 'expected' 1 Answer
why do i keep getting compiler errors even though my script has no errors? 2 Answers
Why isnt the Unity tutorial working for me? 2 Answers
need help with errors,Need help with unity version and errors 1 Answer