Instantiated class with “new” keyword is null after Start method has ended and button is pressed. Unity3d
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 4:52
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 19:22
Ruzihm
2,81711521
2,81711521
asked Nov 19 at 19:12
Jimmyt1988
8,9912791162
8,9912791162
Please don't add code as image but as text to your question.
– derHugo
Nov 20 at 4:52
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00
add a comment |
Please don't add code as image but as text to your question.
– derHugo
Nov 20 at 4:52
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00
Please don't add code as image but as text to your question.
– derHugo
Nov 20 at 4:52
Please don't add code as image but as text to your question.
– derHugo
Nov 20 at 4:52
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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 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 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 at 21:04
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 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 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 at 21:04
add a comment |
up vote
1
down vote
accepted
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 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 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 at 21:04
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 at 19:31
Ruzihm
2,81711521
2,81711521
answered Nov 19 at 19:28
Jimmyt1988
8,9912791162
8,9912791162
2
Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
– Mathias Siig Nørregaard
Nov 19 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 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 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 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 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 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 at 20:07
Also check out the Script Execution Order: docs.unity3d.com/Manual/class-MonoManager.html
– Mathias Siig Nørregaard
Nov 19 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 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 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 at 21:04
Thanks guys. And you'll be happy to know i've removed almost all instances of GameObject.Find. :)
– Jimmyt1988
Nov 19 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 at 4:52
Thanks for the tip derHugo
– Jimmyt1988
Nov 28 at 17:00