- Home /
Attach a Player to a moving GameObject
[Edited for more precision]
Hello !
I've a "playerObject" (that is a prefab, with a rigidbody [gravity set to true]) and when i enter a specific collider (where this script is attached to), i need this "playerObject" to be attached to a Collider (named "balaisColliderObject" in my script) i've add to a broom that is falling.
I've tried this solution :
var playerObject : GameObject;
var pousseurObject : GameObject;
var balaisColliderObject : GameObject;
var attach = false;
function Start () {
playerObject = gameObject.Find("Cesareredim 1");
pousseurObject = gameObject.Find("ColliderPousseur");
balaisColliderObject = gameObject.Find("ColliderBalaisCesare");
}
function OnTriggerEnter (other : Collider){
if(other.collider.gameObject == playerObject){
pousseurObject.rigidbody.AddForce(Vector3.forward * 100);
attach = true;
}
}
function Update (){
if (attach == true){
playerObject.transform.parent = balaisColliderObject.transform;
playerObject.transform.localPosition = balaisColliderObject.OwnerPosition;
}
}
But when i enter the collider, it gives me the "NullReferenceException: Object reference not set to an instance of an object" error. Is there some problem with the objects i'm calling or something like that (i think it's because of the hierarchy change of the object "Cesareredim 1" but i'm not sure of it) ?
Thanks !
gameObject.Find(string) may return null to your var.
(other case: the object was destroyed before trigger)
Nope, it's not that, when i'm in the collider, the inspector shows me that the multiple gameObjects are found. And the objects are not destroyed, i see no reason for that tho happens.
pousseurObject may not have a rigid body
if(other.collider.gameObject == playerObject){
I would use tags for this type of comparison
The error should tell you the line that the error is on, can you point us to that line of code?
Oh sorry i forgot that... its this line (line 28)
playerObject.transform.localPosition = balaisColliderObject.OwnerPosition;
Your answer
Follow this Question
Related Questions
Detecting when Player stoped to push a rigidbody? 3 Answers
Rigidbody Character Jumps Higher With At Least 2 Collisions 3 Answers
Can a character collider be affected by a rigidbody collider? 0 Answers
Make a collider NOT collide with CharacterController 1 Answer
simple collision not working 1 Answer