I've attached my CarController script to a Car prefab. The car has a Rigidbody added by the "Add component" thanks to the inspector menu. But it seems I cannot access the value of any attributes of the Rigidbody in my script. Code (CSharp): private Rigidbody m_Rigidbody; void Start() { m_Rigidbody = GetComponent<Rigidbody>(); } void Update() { if (m_Rigidbody.velocity > 5) { //Do stuff } } > NullReferenceException: Object reference not set to an instance of an object
Because you don't post the full error message I'd have to guess that the error is reported on Line 10. This is because the GameObject that this script is on doesn't have a Rigidbody on it so "m_Rigidbody" is null.
Sorry I didn't want to flood the post with 100 lines of code... Yes it is on line 10. How come it's null, since my GameObject/prefab has one? Here is what I have in my GameObject inspector. If you want the full code, I can upload it if you want.
I think the problem is not this. The Rigidbody is good, but it's link to the logic of my game. I'm using a python client to spawn a Car, so I used a Mains Thread Dispatcher to Instantiate Cars in Threads. And I think o'm not very Thread Safe on what i'm doing. Thanks anyway