- Home /
Parenting 1 object/Removing child/ pick up
Hi there,
I've been thrown into the deep end. My teacher has made our class create a game in Unity after 3 weeks of very basic Javascript "training" (creating basic scripts), hence I know next to nothing about Unity and scripting in the game. I'm trying to create a pick up script using parenting. So far, I have this:
function OnTriggerEnter (other : Collider) {
transform.parent = Camera.main.transform;
}
It works fairly okay, as in I would probably pass the assignment if I used it. However, I would like to make it better. Is there a way to make it only be able to parent 1 object as I would like to have multiple "children" in the level, but the player can only hold onto 1 at a time. Also, is there a way of removing children after I've parented them. And finally, is there a way to position the object once I've parented it so it appears in the middle of the screen, instead of where ever the player collides into the trigger.
I know I sound like a noob, but that's what I am. Programming is not something I see myself doing in the future however I would like to pass this assignment.
Any help will be greatly appreciated. Thanks in advance.
I know this isn't an answer but I'm having the same issue. I want the characters of my game to move with a platform if on it but I can't do it with multiple children. It only works with one!
Answer by aldonaletto · Dec 21, 2011 at 12:46 PM
If all "pickable" objects will use this same script, you can use a static variable to count how many objects are currently in hand, disallowing picking when the max is reached (1 in your case). A static variable is unique: you may have lots of pickable items, but the pickCount variable is the same for all of them:
static var pickCount: int = 0; // global variable that counts currently picked items var maxItems: int = 1; // max items that can be hold var pickPos: Vector3 = Vector3(0, 0.5, 2); // define position relative to player
function OnTriggerEnter (other : Collider) { if (other.tag == "Player"){ // only be picked by the player! if (pickCount < maxItems){ // if player hand not full yet... transform.parent = Camera.main.transform; // pick this object... transform.localPosition = pickPos; // and place it in front of the camera pickCount += 1; // count the item } } }
function DropItem(){ // when releasing the item... transform.parent = null; // set parent to null... pickCount -= 1; // and remember to decrement the pick counter } Notice that I'm comparing tags to check if the object that entered the trigger is actually the player - this disallows other moving things (like lost bullets or enemies) to pick the items. Remember to set the player tag to Player or this script will not work (select the player and click the Tag button in the Inspector, selecting the tag "Player").
Your answer
Follow this Question
Related Questions
How to make multiple gameobjects child of one and the same transform (SCRIPT) 2 Answers
GetComponent searches an objects child instead of the object 2 Answers
How to check parent value instead name value(more details in post) 3 Answers
Adding child Colliders to your parent object's OnTriggerStay 1 Answer