I’m using the Unity standard asset of a car and running through Jimmy Vegas’ racing tutorial. I want to make the CarController script inactive until a countdown finishes. If I deactivate the script in the Inspector window and then try to run my scene, I get the following error:
NullReferenceException: Object reference not set to an instance of an object
UnityStandardAssets.Vehicles.Car.CarController.get_CurrentSpeed () (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:53)
UnityStandardAssets.Vehicles.Car.CarController.ApplyDrive (Single accel, Single footbrake) (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:224)
UnityStandardAssets.Vehicles.Car.CarController.Move (Single steering, Single accel, Single footbrake, Single handbrake) (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:153)
UnityStandardAssets.Vehicles.Car.CarUserControl.FixedUpdate () (at Assets/Standard Assets/Vehicles/Car/Scripts/CarUserControl.cs:27)
It seems like if CarController isn’t active, it shouldn’t even be executing and finding itself to be null, right? Is there a more “proper” way to inactivate a controller script until some condition is true?
I found the issue myself (read the errors, stupid!). The last line of that error references Car.CarUserControl, not Car.CarController. That script is still active, and it references the (inactive) CarController script.
I switched my setup to have CarUserControl inactive instead of CarController, and that solved my problem.
This error is occurring because of the m_rigidbody is returning null. Set the Rigidbody component reference to the m_Rigidbody in the Awake method ( and remove/comment the GetComponent() line in the CarController Start Method). That will resolve the error. Since Awake method is called whether GameObject is active/inactive.
Pessoal nao sei quem ainda está tendo estes problemas, mais eu tive e resolvi da seguinte forma:
notei que meu Carro(Player) tinha varios scripts acoplados em si, cada um com suas funções (ex: destruição, Liga/Desliga, outras funções nas quais vc também incrementou no seu carro), eu desativei todos os scripts do Carro(Player) e deixei apenas os de controles, e fui testando, ao iniciar a gameplay eu ia ligando manual um por um para ver se estava dando interferencia, e descobri que meu script (InputSystem) que faz a ligaçao entre o kayboard e o Touch estava dando interferencia, caso se eu ligasse antes do CarController ele acusava o erro que muitos estavam tendo, então coloquei um tempo de 0.9f de segundos para ligar apos o CarController, e agora está funcionando direitinho, posso até acrescentar mais alguma coisa no script que faz essa ligação de componente que nao da nenhuma interferencia, e também não precisei acrescentar o script padrão do CarUserControl no meu Player que no meu caso não tem utilidade alguma, pode ser que outras versões do unity seja necessária que foi no caso da Unity5 pelo menos eu tive este problema nesta versão, então migrei para a 2018 pois a 2019 não gostei nada da interface preta rsrsrs talvez dava pra mudar e eu nao sabia ksks mais gostei da 2018 e estou usando atualmente até pegar um pc bom e usar a unity 2020, mais enfim esta foi minha experiencia com este erro que ficou me solando durante 2 dias seguidos, era um stress…
Mano eu não sei explicar muito bem, mais deixo aqui oque resolveu pra mim espero que ajude outros…