I was getting Missing Reference Exception within my Audio Source when I change my game scene and again move back to the same scene.
Before I change the main menu scene, it is working fine but after changing and moving back to the main menu scene, it is started showing this exception.
Here is the code I have written for AudioManager:
public class AudioManager : MonoBehaviour
{
static AudioManager instance;
//
[SerializeField] AudioClip buttonClickClip;
[SerializeField] AudioSource myAudioSource;
private void Awake()
{
instance = this;
}
public static AudioManager Instance
{
get
{
return instance;
}
}
public void PlayButtonClickSound()
{
if (GameManager.Instance.IsEnableSounds)
myAudioSource.PlayOneShot(buttonClickClip);
}
}
Here is the code that I wrote for DontDestroyOnLoad purpose:
public class DontDetroyOnLoad : MonoBehaviour
{
private static bool created = false;
void Awake()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
}
else
Destroy(this.gameObject);
}
}
Now please give me some suggestion to solve this problem.
You are referencing the AudioManager that you are destroying in the script that is trying to play the sound.
Think of it this way. You have
AudioManager A - DontDestroyOnLoad
AudioManager B - That gets destroyed cause A exists
In your scripts you are referencing AudioManager A when you first start up. Then when you leave the Scene and return you are now referencing AudioManager B, which got destroyed because A exists. All you need to do is always reference AudioManager A, not B.
Appreciate the answer from #jfish. That clearly describes the situation.
Here let me elaborate mine, on the example of using DontDestroyOnLoad
[SerializeField] AudioSource audioSource;
void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
if (objs.Length > 1)
{
Destroy(this.gameObject);
//Here, get the previous audioSource from previous game object
audioSource = objs[0].GetComponent<AudioSource>();
}
DontDestroyOnLoad(this.gameObject);
}
On first load, it gets from Editor.
Subsequently, it gets from previous game object.
Related
I am trying to use GameObject.Find within a prefab to reference an object that exists in the scene. The name of the object is exactly the same as the actual object.
LoseLives loseLives;
GameObject loseLivesing;
private void Awake()
{
loseLivesing = GameObject.Find("MasterManager");
}
private void Start()
{
loseLives = loseLivesing.GetComponent<LoseLives>();
target = Waypoints.waypoints[0];
}
loseLives.LoseLife();
Null Reference
I get a null reference when "loseLives.LoseLife();" runs.
Any help with solving the no reference problem would be greatly appreciated!
If you think there is a better way to create the connection between a script on a prefab and a script on a in-scene gameObject could you please explain it or point me to a tutorial, because in all my research I did not have any luck.
Thank you!
It seems like your LoseLives is a singular, constant object.
First Method
Simply making use of the inspector by exposing the fields.
public class YourScript : MonoBehaviour {
[SerializeField]
private LoseLives loseLives;
// ..
private void Start() {
loseLives.LoseLife();
}
// ..
}
It comes with the problem of needing to constantly drag your constant object into the inspector whenever you need it.
For a Prefab, it works if the target object is part of the Prefab, or the Prefab is generated in the same scene as the targeted object.
Second Method
Using singletons.
LoseLives.cs
public class LoseLives : MonoBehaviour {
public static LoseLives Instance {
get => Instance;
}
private static LoseLives instance;
private void Start() {
instance = this;
}
public void LoseLife() {
// ...
}
}
YourScript.cs
public class YourScript : MonoBehaviour {
// ..
private void Start() {
LoseLives.Instance.LoseLife();
}
// ..
}
This works if you have a global, singular instance of LoseLives object.
Even though it might not be the best approach, I found a workaround for my issue.
I am using a static variable that resides in the other script (CurrencyManager), calling it from the LoseLives script.
Instead of:
loseLives.LoseLife();
I do:
CurrencyManager.playerHealth -= 1;
Where CurrencyMananger is the other script and playerHealth is a static variable that is in the CurrencyManager script. This works because a static variable does not require the objects to be referenced in the inspector.
From now on, when I have a problem where I want to reference a gameObject within a script that is on a prefab, I will consider using static variables in my solution.
When I enter a collider for a certain game object I would like to play a different sound according to whether its the first time entering the sphere collider or the 2nd or 3rd time. Can this all be written in the same script?
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
//attach in inspector or make private and get with code in the Start()
public AudioSource audioSource1;
public AudioSource audioSource2;
void Start() {
// get audioSources optional
}
int colCounter = 0;
void OnCollisionEnter(Collision collision) {
colCounter++;
if (colCounter == 1)
audioSource1.Play();
else
audioSource2.Play();
}
}
public class GameControl : NetworkBehaviour {
public GameObject localPlayer;
[TargetRpc]
public void TargetGetLocalPlayer(NetworkConnection conn)
{
localPlayer = GameObject.Find("Local");
}
public override void OnStartServer()
{
base.OnStartServer();
TargetGetLocalPlayer(connectionToClient);
}
}
i have a script attached to a server object which should be able to grab the local player GameObject (which i denoted by changing it's name to 'Local' once it's spawned in another script) from the client but when i try to call TargetGetLocalPlayer , i get the following error :
Exception in OnStartServer:Object reference not set to an instance of an object at
UnityEngine.Networking.NetworkBehaviour.SendTargetRPCInternal
(UnityEngine.Networking.NetworkConnection conn, UnityEngine.Networking.NetworkWriter writer,
System.Int32 channelId, System.String rpcName) [0x0002e]
i am totally new to networking in unity and i feel like i should have gone with photon instead of unet , it seems like no one is interested in unet anymore and the docs suck at explaining anything and i will be very grateful if anyone could answer me , thanks in advance
I think a better solution would be to attach a script to each player. and make it so that when a "new" player joins it runs a method in your GameControl to add the player to a player list. like this:
//this is on your gameControl.
public List<Player> players;
public void AddPlayer(Player player)
{
players.Add(player);
}
this is working if you have your GameControl as a singleton. if you do not know how to do that check the last piece of code.
//this is on your Player script called player(if you have another name change all
//Player scripts in here to that name
public void Start()
{
GameControl.AddPlayer(this);
}
Or
Instead of making the players list a List you can make it a dictionary and make a key for each player who joins to make it more accesible.
How to make a script a singleton and why.
why:
if you have a manager class/script you always want there to only be ONE instance of it so you can call its methods easily and without problems.
How:
Basically you make it so that Only THIS script can change values and variables in the manager, and other scripts can get/call methods and functions. this makes it easily accesible and you will have less problems.
private static GameControl _GameControl;
private Player player;
public static GameControl gameControl
{
get
{
if(_GameControl == null)
{
Debug.LogError("Game Control is null");
}
return _GameControl;
}
}
void Awake()
{
_GameControl = this;
player = GameObject.Find("Player").GetComponent<Player>();
}
Well i see what you mean, well create a method that can be run by any script like a singleton. then you pass in the gameobject that you want to add like this:
public class GameManager
{
public GameObject _player;
//this is a singleton.
private static GameManager _gm;
public static GameManager gameManager
{
get
{
if(_gm == null)
{
Debug.LogError("Game manager is null");
}
return _gm;
}
}
void awake()
{
_gm = this;
}
void GetPlayer(GameObject player)
{
_player = player;
}
void AddPlayer(GameObject player)
{
//add it to whatever you want to.
}
}
call the method this way:
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameManager.gameManager.GetPlayer(this.gameObject);
}
}
I have a weapon that I want to instantiate an object (In this case a bullet), then wait until that bullet has hit an object before the weapon is allowed to instantiate another object.
I am currently doing this by having this on the weapon script:
public class weaponScript : MonoBehaviour
{
public gameobject projectilePrefab;
public bool launchable;
void Update{
if(launchable){
Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
launchable = false;
}
}
And this on the bullet script:
public class projectile : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D other){
weaponScript.launchable = true;
}
}
This would work perfect for my needs, however this doesn't work because the projectile has no definition of what weaponScript is, therefore it can't set the launchable variable on the weaponScript to true.
I could use the FindObjectOdType() function, and that works when you have one weapon in the scene, but once you have more than one weaponScript in the scene at a time, you start to having problems determining who is who.
Is there a way for the weaponScript to set itself as a variable when it instantiates an object so it would be like:
public class weaponScript : MonoBehaviour
{
public gameobject projectilePrefab;
public bool launchable;
void Update{
if(launchable){
Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
[InstanciatedObjectHere].parentWeapon = this.gameobject;
launchable = false;
}
}
That way all the projectile has to do is:
public class projectile : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D other){
parentWeapon.launchable = true;
}
}
Solution:
I know Pastebin won't keep the script forever, so I'm going to put the answer here as an edit: (plus it makes it easier for some else who might stumble upon this to read)
I followed Peridis's answer, but it didn't work immediately, so I ended up tweaking it and came up with this:
public class weaponScript : MonoBehaviour
{
public gameobject projectilePrefab;
public bool launchable;
private projectile projectileScript;
void Update{
if(launchable){
GameObject projectileIntantiated = Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
projectileScript = projectileIntantiated.GetComponent<projectile>();
projectileScript.parentWeapon = this.gameObject.GetComponent<weaponScript>();
launchable = false;
}
}
public class projectile : MonoBehaviour
{
public weaponScript parentWeapon;
void OnCollisionEnter2D(Collision2D other){
parentWeapon.launchable = true;
}
}
Thank you for the help Peridis!
You can create a variable of type GameObject when Instantiating the projectile
GameObject projectileIntantiated = Instantiate(projectilePrefab, firePoint.transform.position, transform.rotation);
projectileIntantiated.GetComponent<projectile>().parentWeapon = this.gameobject;
I have a baseclass that inherits from Monobehaviour. How do i cast my monobehaviour to the base class when finding it in the hierarchy?
GameManager : MonoBehaviour {
public MonoBaseClass MyThing;
void Awake() {
MyThing = GameObject.Find("Child") as MonoBaseClass;
}
}
MonoBaseClass : MonoBehaviour {
public void BaseClassMethod() {}
}
GameObject.Find returns a GameObject, a MonoBehaviour is a component of a GameObject. That's why you can't cast the GameObject to the MonoBaseClass.
Instead you have to get a reference of the GameObject and then get the Component:
GameObject childGameObject = GameObject.Find("Child");
MyThing = childGameObject.GetComponent<MonoBaseClass>();
You need to use FindObjectOfType<MonoBaseClass>(), i.e.:
void Awake() {
MyThing = FindObjectOfType<MonoBaseClass>();
}
Problem with both Find and FindObjectOfType is: They are quite slow and you will get the first hit from the entire scene.
If the Component you are looking for is on a Gameobject which is a child of the current GameObject (which seems the case) than you can just use:
MyThing = GetComponentInChildren<MonoBaseClass>();
https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html
Of course this will anyway still only get the first hit. For more use an array and GetComponentsInChildren<T>()