[ask] making a rock falling down using rigidbody

hi, i’m want to make a rock that falling down from above when i move into some trigger… but it seem not working here is the code i’m using in the trigger sphere

var rock1:GameObject;

function OnTriggerEnter (theCollider:Collider){
	if(rock1.GetComponents(Rigidbody)){
		rock1.rigidbody.useGravity = true;
		rock1.rigidbody.WakeUp();
		Debug.Log("hit");
	}

}

but it seem not working, when i move into the trigger the rock just didn’t fall…

you if-statement is a bit wonky, thats why…

When something enters the trigger area the function “OnTriggerEnter” will get launched. It will give you the object “theCollider” that you have specified in the parantheses. From this “Collider” object you can extract information about WHAT it is, that has set off your trigger.

if(theCollider.gameObject.name == "Your rocks name in the inspector"){
    //Then something will happen here
}

getting the name is one way, or you could get the tag of the object:

if(theCollider.gameObject.tag == "The tag you have on your object"){
    //Then something will happen here
}

After you have identified WHAT it is that has collided, you can manipulate the object at hand by writing

theCollider.gameObject."WhateverYouWantToDoWithYourObject"

Happy coding /T

you get it wrong, i don’t collide to the rock but to the invisible sphere so when i collide the sphere, the rock above me falling…

It could be you’re if statement. GetComponents(type) returns an array, which you are using as a bool.
You could use something like
if(rock1.rigidbody){
rock1.rigidbody.useGravity = true;
}

but if the console is printing “hit”, then it isn’t that. You just have to experiment with the rock.

already tried so many way to but until now it just not working…

My answer is ONE ANSWER and is the correct way cause if you write it your way, anything can trigger it. Sure if anything can trigger it. What is wrong with your if-statement is that you are getting

Rigidbody

that is the main class and not the component.

if you READ THE DOCUMENTATION

you’ll se that GetComponent(string type);

so write

getComponent(“rigidbody”);

before posting, check the documentation and make sure you’ve used the functions in a correct way.

/Happy coding

You have provided a solution, which is incorrect in this users case… you havnt provided ‘the only correct solution’

Laurensius, you should probably read this document… http://docs.unity3d.com/Documentation/Manual/Physics.html

There are a few potential reasons it might not be working, main reasons being that the setup of the colliders/rigidbodies isnt correct.

You can make it easier for yourself if you put some debugging code in…ie

var rock1:GameObject;

function OnTriggerEnter (theCollider:Collider){
    Debug.Log("OnTriggerEnter - "+ theCollider.name);
    
    if(rock1 == null)
        Debug.Log("Rock1 is null!"
    else if(rock1.rigidbody == null)
        Debug.Log("Rock1 has no rigidbody");
    else 
    {
         Debug.Log("Activating Rock");
         rock1.rigidbody.useGravity = true;
         rock1.rigidbody.isKinematic = false;
    }

using that code, you will at least know if youve setup stuff correctly. If you dont see any messages at all (namely - OnTriggerEnter), you know that one of your objects doesnt have the correct rigidbody/collider setup.

thank you for helping me with all method to detect what’s wrong and i think the one that makes it wrong is when i apply to my rock prefabs mesh collider/box collider and also rigidbody, as soon as i start the game it just falling down out of the scene… and i don’t know why… it suppose to be colliding with the floor it self

edited: i keeping attention to inspector and notice that every time the game start the rigibody usegravity value become true… but i already make it false and i tried it so many times now

okay after keep trying to check i already found the solution… it just me that being dumb… i put my rock with box collider inside sphere collider with istrigger enabled and script that enable gravity attached into it…

thank you so much for all of your effor helping me :smile: