Hi all. Long time lurker, first time posting. I have a unique problem. I have 3 objects with the same script attached. I am trying to pick them up and throw them. It works when I do it to the first object I created, but it won’t do it to the duplicates, even though debugging makes it show the correct object name (object1, object2, object3). The first IF statement checks out perfectly, however the other 2 IF statements revert back to the originally named object (object1). Why would this happen?
#pragma strict
var playerDist : float;
var pickupDistance : float = 2.0;
var throwForce : int = 10;
var playerTrans : Transform;
var objectGoal : Transform;
var objectToBe : GameObject;
var playerAboveObj : float;
static var beingHeld : boolean;
function Start ()
{
playerTrans = gameObject.FindWithTag(“Player”).transform;
objectGoal = gameObject.Find(“Launcher”).transform;
beingHeld = false;
}
function simpleWait(waitTime : float)
{
yield WaitForSeconds(waitTime);
}
function pickupObject()
{
playerDist = Vector3.Distance(playerTrans.position, this.gameObject.transform.position);
playerAboveObj = Mathf.Abs((playerTrans.position - this.gameObject.transform.position).y);
if(Input.GetButtonDown(“Fire2”) playerDist < pickupDistance beingHeld == false playerAboveObj < 0.9)
{
this.gameObject.collider.enabled = false;
this.gameObject.rigidbody.isKinematic = true;
this.gameObject.rigidbody.useGravity = false;
this.gameObject.transform.parent = objectGoal.gameObject.transform;
this.gameObject.transform.localPosition = Vector3(objectGoal.localPosition.x, objectGoal.localPosition.y + 0.5,
objectGoal.localPosition.z + 1);
Debug.Log(this.gameObject.name);
beingHeld = true;
}
if(Input.GetButtonDown(“Fire1”) beingHeld == true)
{
Debug.Log(“LEFT CLICK”);
pickupDistance = 0.0;
beingHeld = true;
StartCoroutine(simpleWait(0.5));
this.gameObject.rigidbody.isKinematic = false;
this.gameObject.rigidbody.useGravity = true;
this.gameObject.collider.enabled = true;
this.gameObject.transform.parent = null;
this.gameObject.rigidbody.AddForce(objectGoal.forward * throwForce, ForceMode.Impulse);
StartCoroutine(simpleWait(0.5));
pickupDistance = 2.0;
beingHeld = false;
Debug.Log(this.gameObject.name);
}
if(Input.GetButtonUp(“Fire2”) beingHeld == true)
{
Debug.Log(“DROPPED”);
this.gameObject.rigidbody.isKinematic = false;
this.gameObject.rigidbody.useGravity = true;
this.gameObject.collider.enabled = true;
this.gameObject.transform.parent = null;
beingHeld = false;
Debug.Log(this.gameObject.name);
}
}
function Update ()
{
pickupObject();
}
Sorry, don’t know how to format the code properly. Basically the objects pick up properly, but they won’t throw. Only object1 becomes affected by addforce or gravity, etc. Somewhere in the code the object name is reverting back to object1, but I can’t pinpoint it. Thanks for the help!