So I have a flashlight which is automatically off in every scene, I want the flashlight to stay on when moving scenes, I tried ‘dontdestroyonload’ but that didn’t work unfortunately.
if someone has any idea help would be very much appreciated
So I have a flashlight which is automatically off in every scene, I want the flashlight to stay on when moving scenes, I tried ‘dontdestroyonload’ but that didn’t work unfortunately.
if someone has any idea help would be very much appreciated
Store its on/off-ness somewhere else, such as in a GameManager type construct.
ULTRA-simple static solution to a GameManager:
OR for a more-complex “lives as a MonoBehaviour or ScriptableObject” solution…
Simple Singleton (UnitySingleton):
Some super-simple Singleton examples to take and modify:
Simple Unity3D Singleton (no predefined data):
Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:
These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance
Alternately you could start one up with a RuntimeInitializeOnLoad attribute.
The above solutions can be modified to additively load a scene instead, BUT scenes do not load until end of frame, which means your static factory cannot return the instance that will be in the to-be-loaded scene. This is a minor limitation that is simple to work around.
If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:
public void DestroyThyself()
{
Destroy(gameObject);
Instance = null; // because destroy doesn't happen until end of frame
}
There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.
OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.
And finally there’s always just a simple “static locator” pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.
WARNING: this does NOT control their uniqueness.
WARNING: this does NOT control their lifecycle.
public static MyClass Instance { get; private set; }
void OnEnable()
{
Instance = this;
}
void OnDisable()
{
Instance = null; // keep everybody honest when we're not around
}
Anyone can get at it via MyClass.Instance., but only while it exists.
For future reference:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
please don’t photograph your screen, that’s even worse than a screenshot ![]()
Code is plain TEXT - you can simply copy/paste that you know. ![]()
Thank you, I decided to create a 3D horror game which I started working on 3 days ago, I’ve never coded before so it’s all pretty new to me and I’ve been stuck on this since day 2, any chance you know of a youtube video I could check out?
I like this guy’s approach:
Imphenzia: How Did I Learn To Make Games:
He has a lot of very good specific videos on coding too:
Imphenzia / imphenzia - super-basic Unity tutorial:
Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly, two (2) simple steps to success:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!
If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
Finally, when you have errors, don’t post here… just go fix your errors! Here’s how:
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
Thanks alot I’ll check it out immediately, I’ll try to figure it out myself and if I really don’t know how or if google doesn’t know I’ll come back here!
Alright so I’m back again with a really weird issue, I had a code which I wrote on day 1, the code worked perfectly untill 10 minutes ago, all of a sudden it gives an error 'object reference not set to an instance of an object line 16, I looked it up and understand what it means, however I have no idea how to fix it since the code worked perfectly 10 minutes ago without changing anything it stopped working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.UI;
public class StartText : MonoBehaviour
{
public GameObject ThePlayer;
public GameObject Start;
public GameObject TalkBox;
void OnTriggerEnter () {
ThePlayer.GetComponent<FirstPersonController> ().enabled = false;
StartCoroutine (ScenePlayer ());
}
IEnumerator ScenePlayer() {
Start.SetActive (true);
yield return new WaitForSeconds (10f);
Start.SetActive (false);
ThePlayer.GetComponent<FirstPersonController> ().enabled = true;
TalkBox.SetActive (false);
}
}
I am very confused as to why it stopped working without me changing anything, I tried turning the getcomponand from true to false and it did percisely 0, do you have any idea what it could be?
I messaged you to quickly, after 30 minutes of searching and tracing all of my steps it happened to be a duplicate file which ruined all of my other scripts
Good find! It feels nice to track it down yourself, doesn’t it? ![]()
In any case, for future reference as I am sure you already saw elsewhere for NullReferenceException:
The answer is always the same… ALWAYS!
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success: