I am trying to make a script that changes on the fly the mesh collider to be convex.
Here is my attempt:
rigidbody.Sleep();
rigidbody.mass = 0.5;
public var MeshCollider;
MeshCollider.convex = true;
function OnCollisionEnter(collision : Collision) {
if (collision.rigidbody)
Destroy (rigidbody);
}
Here are the errors I recieve:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, System.Object[ ] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[ ] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
MassConvex-Crack.Main () (at Assets\Scripts\2D\Physics\MassConvex-Crack.js:6)
It’s exactly what the error says, your object is not set to anything, it is null. Just declaring the variable MeshCollider doesn’t fill it with anything.
// This means you can't place the script on an object that doesn't have a mesh collider component, which should help with future accidental errors
@script RequireComponent(MeshCollider)
// Note that using the Capitilised name will actually refer to the type, not the variable.
public var meshCollider : MeshCollider;
function Start()
{
meshCollider = GetComponent(MeshCollider);
meshCollider.convex = true;
}
That should remove your error, however I’m not sure that changing it at runtime is the best move performance wise. Give it a go though, I’ve never really tried it.
As you can see I am still a newbie at scripting. I have about 500 objects that need to be set convex, I noticed that selecting all of them in the inspector and changing them to convex does not work, since it only does one. That’s why I turned to scripting because it can be batch applied.
Unless there’s an alternative to changing each one individually I’ll stick with scripting.
I forgot to remove the Destroy Rigidbody section before I posted.
I suppose if you just call it during the Start or Awake functions it should be alright. You could also write a quick editor script that does the batch for you, then you could just do it once in the editor and not have extended load times.
It should. Check out the editor classes here in the documentation. It’s quite easy to get a custom window up and add a few buttons to handle your own functions.