Instantiated class with “new” keyword is null after Start method has ended and button is pressed. Unity3d












0














I have a SaveController, it looks like this:



enter image description here



In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:



enter image description here



When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...



enter image description here



public void MusicOn(bool on)
{
saveController.MusicOn(on);
}


I've obviously not understood the application flow, so wondered if anyone has an idea on what might be going wrong. Which method should i instantiate the SaveController?



P.S I know, GameObject.Find, bleurgh... i'm refactoring, no wuckers.










share|improve this question
























  • Please don't add code as image but as text to your question.
    – derHugo
    Nov 20 '18 at 4:52










  • Thanks for the tip derHugo
    – Jimmyt1988
    Nov 28 '18 at 17:00
















0














I have a SaveController, it looks like this:



enter image description here



In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:



enter image description here



When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...



enter image description here



public void MusicOn(bool on)
{
saveController.MusicOn(on);
}


I've obviously not understood the application flow, so wondered if anyone has an idea on what might be going wrong. Which method should i instantiate the SaveController?



P.S I know, GameObject.Find, bleurgh... i'm refactoring, no wuckers.










share|improve this question
























  • Please don't add code as image but as text to your question.
    – derHugo
    Nov 20 '18 at 4:52










  • Thanks for the tip derHugo
    – Jimmyt1988
    Nov 28 '18 at 17:00














0












0








0







I have a SaveController, it looks like this:



enter image description here



In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:



enter image description here



When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...



enter image description here



public void MusicOn(bool on)
{
saveController.MusicOn(on);
}


I've obviously not understood the application flow, so wondered if anyone has an idea on what might be going wrong. Which method should i instantiate the SaveController?



P.S I know, GameObject.Find, bleurgh... i'm refactoring, no wuckers.










share|improve this question















I have a SaveController, it looks like this:



enter image description here



In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:



enter image description here



When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...



enter image description here



public void MusicOn(bool on)
{
saveController.MusicOn(on);
}


I've obviously not understood the application flow, so wondered if anyone has an idea on what might be going wrong. Which method should i instantiate the SaveController?



P.S I know, GameObject.Find, bleurgh... i'm refactoring, no wuckers.







c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:22









Ruzihm

3,53111627




3,53111627










asked Nov 19 '18 at 19:12









Jimmyt1988Jimmyt1988

9,0482791162




9,0482791162












  • Please don't add code as image but as text to your question.
    – derHugo
    Nov 20 '18 at 4:52










  • Thanks for the tip derHugo
    – Jimmyt1988
    Nov 28 '18 at 17:00


















  • Please don't add code as image but as text to your question.
    – derHugo
    Nov 20 '18 at 4:52










  • Thanks for the tip derHugo
    – Jimmyt1988
    Nov 28 '18 at 17:00
















Please don't add code as image but as text to your question.
– derHugo
Nov 20 '18 at 4:52




Please don't add code as image but as text to your question.
– derHugo
Nov 20 '18 at 4:52












Thanks for the tip derHugo
– Jimmyt1988
Nov 28 '18 at 17:00




Thanks for the tip derHugo
– Jimmyt1988
Nov 28 '18 at 17:00












1 Answer
1






active

oldest

votes


















1














Looks like Awake was the method i was looking for.



Awake is called before Start, so doing the saveController instantiation in Awake allows for the instantiation to happen before MusicOn is called.



void Awake() 
{
saveController = new SaveController();
}


source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start






share|improve this answer



















  • 2




    Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:07










  • Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:08






  • 1




    Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
    – Jimmyt1988
    Nov 19 '18 at 21:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381164%2finstantiated-class-with-new-keyword-is-null-after-start-method-has-ended-and-b%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Looks like Awake was the method i was looking for.



Awake is called before Start, so doing the saveController instantiation in Awake allows for the instantiation to happen before MusicOn is called.



void Awake() 
{
saveController = new SaveController();
}


source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start






share|improve this answer



















  • 2




    Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:07










  • Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:08






  • 1




    Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
    – Jimmyt1988
    Nov 19 '18 at 21:04
















1














Looks like Awake was the method i was looking for.



Awake is called before Start, so doing the saveController instantiation in Awake allows for the instantiation to happen before MusicOn is called.



void Awake() 
{
saveController = new SaveController();
}


source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start






share|improve this answer



















  • 2




    Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:07










  • Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:08






  • 1




    Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
    – Jimmyt1988
    Nov 19 '18 at 21:04














1












1








1






Looks like Awake was the method i was looking for.



Awake is called before Start, so doing the saveController instantiation in Awake allows for the instantiation to happen before MusicOn is called.



void Awake() 
{
saveController = new SaveController();
}


source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start






share|improve this answer














Looks like Awake was the method i was looking for.



Awake is called before Start, so doing the saveController instantiation in Awake allows for the instantiation to happen before MusicOn is called.



void Awake() 
{
saveController = new SaveController();
}


source: https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 19:31









Ruzihm

3,53111627




3,53111627










answered Nov 19 '18 at 19:28









Jimmyt1988Jimmyt1988

9,0482791162




9,0482791162








  • 2




    Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:07










  • Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:08






  • 1




    Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
    – Jimmyt1988
    Nov 19 '18 at 21:04














  • 2




    Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:07










  • Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
    – Mathias Siig Nørregaard
    Nov 19 '18 at 20:08






  • 1




    Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
    – Jimmyt1988
    Nov 19 '18 at 21:04








2




2




Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
– Mathias Siig Nørregaard
Nov 19 '18 at 20:07




Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
– Mathias Siig Nørregaard
Nov 19 '18 at 20:07












Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
– Mathias Siig Nørregaard
Nov 19 '18 at 20:08




Another option is also wrapping saveController in a property that checks if saveController is null, then constructing an instance if it is and returning saveController.
– Mathias Siig Nørregaard
Nov 19 '18 at 20:08




1




1




Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
– Jimmyt1988
Nov 19 '18 at 21:04




Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
– Jimmyt1988
Nov 19 '18 at 21:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381164%2finstantiated-class-with-new-keyword-is-null-after-start-method-has-ended-and-b%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]