Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AshwinTheGammer · Apr 23, 2020 at 04:43 PM · scene-switchingdontdestroyonloadcallaccessing from any script

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!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AshwinTheGammer · Apr 25, 2020 at 04:22 PM 0
Share

I've already solved the problem. I've done exactly like you. Since, you've added right answer so I accepted it. Thank you!

avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image K00KIE · Apr 23, 2020 at 06:33 PM 0
Share

if singleton does not work consider watching : https://www.youtube.com/watch?v=ofCLJsSUom0 from 1:42 to 2:10

avatar image AshwinTheGammer · Apr 24, 2020 at 06:35 AM 0
Share

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.

avatar image K00KIE AshwinTheGammer · Apr 24, 2020 at 08:05 AM 0
Share

have you tried the method in the recommended video. For me, that system has always worked(the one shown in video).

avatar image AshwinTheGammer K00KIE · Apr 25, 2020 at 12:52 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image K00KIE · Apr 23, 2020 at 06:32 PM 0
Share

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

avatar image AshwinTheGammer · Apr 24, 2020 at 06:36 AM 0
Share

can you show me an example? how you do that?

avatar image rh_galaxy AshwinTheGammer · Apr 24, 2020 at 08:48 AM 0
Share

See my answer to this question.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges