Instantiated class with “new” keyword is null after Start method has ended and button is pressed. Unity3d
I have a SaveController, it looks like this:
In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:
When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...
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
add a comment |
I have a SaveController, it looks like this:
In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:
When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...
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
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
add a comment |
I have a SaveController, it looks like this:
In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:
When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...
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
I have a SaveController, it looks like this:
In the Start method of a MainMenuLogic class, I instantiate an instance of SaveController:
When the game starts, MusicOn method has been called before the Start method is called so the soundController is null...
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
c# unity3d
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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