- Home /
Can't call script from another scene using DONTDESTROYONLOAD().
I wanna alter the variable of senstivityX and sensitvityY of player. MouseController script is attached to the player and camera of the player. The player is in another scene. I wanna change the mouse sensitivity from the Main Menu Scene using Slider. I'm using DontDestroyOnLoad() function to accomplish this. But, the problem is show me the error
Object not set to an reference.
Actually, the MENU script (it's in MainMenu scene) tries to search the player within Main Menu scene, whereas player in another scene("FPS_Battle"). I don't know what am I doing wrong. Even though I'm using the DontDestroyOnLoad() function.
Here's menu script which is assigned to an empty gameobject and it's in Main Menu scene.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Menu : MonoBehaviour {
private Slider sens;
private GameObject playerSens;
private GameObject CameraSens;
void Start(){
DontDestroyOnLoad (this.gameObject); //dont destroy this gameobject so that it can be used further in another scene.
sens = GameObject.FindGameObjectWithTag ("sensX").GetComponent<Slider> (); //access the slider compoenent in scene1(menu);
playerSens = GameObject.FindGameObjectWithTag ("Player"); //search for the player gameobect of player in scene2
CameraSens = GameObject.FindGameObjectWithTag ("CameraSens"); //search for the camera gameobject of player in scene2
}
public void Senstivity(){
playerSens.GetComponent<MouseController> ().sensitivityX = sens.value; //change the sensitivity of player - X axis - **
CameraSens.GetComponent<MouseController> ().sensitivityY = sens.value; //change the senstivity of camera - Y axis
}
//When I change the senstivity it shows from error that object not reference set. **
}
}
I don't wanna use STATIC keyword as it poses some problems for me. Thanks!!
Answer by Priyanka-Rajwanshi · Apr 25, 2020 at 03:40 PM
@AshwinTheGammer Since the player doesnt exist in start of Menu.cs ( as player is in scene2), you cannot find player with tag in the Start() of Menu.cs. Here is a workaroud. Take a public variable in Menu.cs
public float sensitivity;
public void Senstivity(){
sensitivity = sens.value;
}
In your next scene, where you can find player, use the following line in Start() of either MouseController.cs or some other script that can access MouseController.cs in Scene2
float sensitivity = FindObjectOfType<Menu>(). sensitivity;
sensitivityX = sensitivity;
sensitivityY = sensitivity;
As you have written DontDestroyOnLoad() in Menu.cs, this script would be available in scene 2 in the start of MouseController.cs and the above snippet would work.
I've already solved the problem. I've done exactly like you. Since, you've added right answer so I accepted it. Thank you!
Answer by K00KIE · Apr 23, 2020 at 05:48 PM
Hi bud,
Gonna need one little detail if you don't mind.
Is DontDestroyOnLoad() working as intended. Also, which part of script is giving the error.
If DontDestroyOnLoad() is not working, please let me know.
if singleton does not work consider watching : https://www.youtube.com/watch?v=ofCLJsSUom0 from 1:42 to 2:10
No its not working and it's showin' error on this line:
playerSens.GetComponent ().sensitivityX = sens.value;
this happens when I try to change the senstivity by slider.
I've seen many people using dontdestroyonload() function to attain this thing.
have you tried the method in the recommended video. For me, that system has always worked(the one shown in video).
Hey bro!! it worked like a charm. I was confused even after watching the video. But anyhow , I made it!! edit your answer so that I can mark it as correct.
If you won't $$anonymous$$d would u join my discord? u really helped me
Answer by rh_galaxy · Apr 23, 2020 at 06:22 PM
At the time of Start() for the Menu no objects in scene2 exists, causing GameObject.FindGameObjectWithTag to fail.
I use a singleton DoNotDestroy GameManager class to handle scene transitions and passing variables between scenes. I also think you will have to use static variables.
Yep. he is right. If your said singleton method does not work, you have to use static variables. I would recommend this video : https://www.youtube.com/watch?v=ofCLJsSUom0 from 1:42 to 2:10
Your answer
Follow this Question
Related Questions
Canvas will only appear on the game screen when its child of a new loaded scene canvas 1 Answer
Question Regarding Don'tDestroyOnLoad Object 1 Answer
Don't destroy on load problem 1 Answer
When die - reset score from the last scene 2 Answers
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer