Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 damisco · Mar 31 at 04:36 PM · unity 5scene-loadingscene-switching

CharacterController on prefab null after changing scenes.,

Hi All,

Im creating a VR game and on my player/XR Origin object I have two things that keep failing. Of note they dont seem to fail at the same time always which makes me think something with how scenes load.

I have a character controller on my prefab and also a button mapping for a jump function.

In the first scene everything works fine. After moving to another scene (but not always) when I get to the scene and press the jump button I get the following two errors:

 MissingReferenceException: The object of type 'CharacterController' has been destroyed but you are still trying to access it.
 Your script should either check if it is null or you should not destroy the object.
 PlayerControllerJump.OnJump (UnityEngine.InputSystem.InputAction+CallbackContext obj) (at Assets/VR_Avatar/PlayerControllerJump.cs:37)
 UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.CallbackArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Utilities/DelegateHelpers.cs:46)
 UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
 UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)

And followed behind it (again not always)

 MissingReferenceException while executing 'performed' callbacks of 'XRI RightHand Interaction/Jump[/OculusTouchControllerOpenXR1/primarybutton]'
 UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
 UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)


Below is the script for jump and also the script for changing scenes.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerControllerJump : MonoBehaviour
 {
     [SerializeField] private InputActionReference jumpActionReference;
     [SerializeField] private InputActionReference exitActionReference;
     [SerializeField] private float jumpHeight;
     private CharacterController controller;
     private Vector3 playerVelocity;
     private bool groundedPlayer;
     private float gravityValue = -9.81f;
 
     void Start()
     {
         controller = GetComponent<CharacterController>();
         jumpActionReference.action.performed += OnJump;
         exitActionReference.action.performed += ExitGame;
     }
 
     // Update is called once per frame
     void Update()
     {
         groundedPlayer = controller.isGrounded;
         if (groundedPlayer && playerVelocity.y < 0)
         {
             playerVelocity.y = 0f;
         }
         playerVelocity.y += gravityValue * Time.deltaTime;
         controller.Move(playerVelocity * Time.deltaTime);
     }
 
     private void OnJump(InputAction.CallbackContext obj)
     {
         if (controller.isGrounded)
         {
             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
         }
     }
 
     private void ExitGame(InputAction.CallbackContext obj)
     {
         Application.Quit();
     }
 }

Script for changing scenes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class commons_zone : MonoBehaviour
 {
     //METHOD 1
      void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.tag == "Player")
          {
              StartCoroutine(ChangeScene(other.gameObject));
          }
      }
 
      IEnumerator ChangeScene(GameObject other)
      {
          SceneManager.LoadScene(4, LoadSceneMode.Additive);
          //Note that SceneManager is keeping it's own array of scenes below. Not to be confused with the build settings list of scenes.
          Scene nextScene = SceneManager.GetSceneAt(1);
          yield return null;
          SceneManager.UnloadScene(0);
      }
 
     //METHOD 2
     /*void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
             SceneManager.LoadScene();
     }*/
 }

Here is what I have tried:

  • Create a prefab in each scene (the setup for the code above currently)

  • tried adding and removing a script on the prefab that has DontDestroyOnLoad() for both the prefab as a whole and just the characterController.

    • Also tried taking prefab out of the other scene and using scenemanager to move it over to the new scene. (not represented in the code anymore).

No progress with either attempts. Any help would be appreciated!

,

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

2 Replies

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

Answer by damisco · Apr 03 at 01:24 PM

I wouldn't say I "fixed" this but I have at least come up with a temporary solution. I realized that when I made the character controller and called DontDestroyOnLoad() on it I still crashed but didn't have the missing ref exception for the char controller on it anymore. It did still the one for the righthand interaction. Looking a little closer at that, I found there was a call being made to verify the status of the XR Interactor Line Visual component from the XR Origin.

Technically if I want to use that component for any reason this is still broken. But for the most part I find it good for testing out VR Rigs and can get away with disabling it for now.

Comment
Add comment · 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
0

Answer by Vannerss · Apr 01 at 02:50 AM

Hello this might not be the right I answer but I feel it might be worth a try. if you add a requirecomponent attribute on top of playercontrollerclass it might fix it. https://docs.unity3d.com/ScriptReference/RequireComponent.html

 [RequireComponent(typeof(CharacterController))]
 public class PlayerControllerJump : MonoBehavior 
 {
         private CharacterController controller;
 
         private void Start()
         {
             controller = GetComponent<CharacterController>();
         }
 }

Also in the case you are not using the character controller default values you can change them in the start method after getting the component.

  [RequireComponent(typeof(CharacterController))]
  public class PlayerControllerJump : MonoBehavior 
  {
          private CharacterController controller;
  
          private void Start()
          {
              controller = GetComponent<CharacterController>();
             controller.slopeLimit = 75;
             controller.stepOffset = 0.7f;
          }
  }


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 damisco · Apr 03 at 12:51 PM 0
Share

So it didn't fix my situation but it got me thinking along the right track. Posting my answer below.

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

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

Can't find game object / script after scene re-enter 1 Answer

Game isn't working the same way before main menu? 0 Answers

Skybox disappears after I switch scene 1 Answer

Can I use one script to toggle between scenes? 1 Answer

Android back button really slow 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