# On collision, spin another object script - help!

So i’ve got two cubes in my scene, “cube” and “spintest”.

I want to make it so when I touch “cube” that “spintest” starts to spin. This is my current script:

``````var boxspin : GameObject;

function OnCollisionEnter(collision : Collision) {

boxspin = GameObject.Find("spintest");

boxspin.transform.Rotate(Time.deltaTime, 2, 0);

}
``````

I’ve tried the exact same script but only with mouse over (and that worked) but I just can’t get it to work for when I touch the box. I’ve also tried function OnTriggerEnter (other: Collider) and making “cube” a trigger but that didn’t work either. Can someone help me out? Would be most appreciated, thanks.

I’m assuming you want it to continue to spin, even after the collision stops.

Put a ‘spin’ variable on the spintest and set it to false.

In OnCollisionEnter, find the spintest and then set that variable to true.

In the Update on spintest, do your rotation only if ‘spin’ is true.

Thanks, I’m close I can feel it haha. So what you’re saying is, have 2 seperate scripts - one on the collision “cube” and the other on “spintest”?

I currently have this on spintest:

``````var spin : boolean = false;

function update () {

if (spin == true);

transform.Rotate(Time.deltaTime, 2, 0);

}
``````

So now I have to make “spin” true from my other OnCollisionEnter script after I find “spintest” ? How do I set the var spin to true from the other script? (I hope i’m asking/doing the correct things, I’m still relativly new to this )

That sounds right, yup.

You should be able to find spintest’s script from the script on the cube, then set it to true. Examples at: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

From those examples, it looks like you can do that. If you can’t do that directly for some reason, you’ll have to write a setter function for spin on spintest’s script like:

``````function setSpin(value:boolean) {
spin=value;
}
``````

(I don’t usually do UnityScript, and I can’t test this right now, but it’s something like that.)

Alright thanks for your help, much appreciated. Haven’t got it working yet but you certainly put me on the right track, i’ll post back here if I have any questions.

Ok, this is slowly driving me insane, so I’m bumping my thread to ask more.

I have this script on my cube that I want to collide with:

``````function OnCollisionEnter(collision : Collision) {

var spin = GameObject.Find("spintest");
spin.GetComponent(Onspinningcube).spin = true();

}
``````

And this on “spintest” who I want to spin around when I collide with the first cube:

``````var spin : boolean = false;

function update () {

if (spin == true);

transform.Rotate(Time.deltaTime, 2, 0);

}
``````

But now I’m getting the error "It is not possible to invoke an expression of type ‘boolean’. " What does this mean? Can someone help me out please?

“true()” should be “true”

Ok thanks, that fixed the error but i’m still actually unable to get the var spin to become true when I collide with the first cube. If I select the “spintest” I want to spin and watch the checkbox next to “spin” in the inspector, it doesn’t become checked when I collide with the first cube.

Update need to be written with capital letter not update.
There is no “;” after an if statement.

Your code is full of mistakes. Make yourself a favor and find an editor who knows syntax for Unityscript. Though seeing you have no clue how to program you shouldn’t do it in Unityscript. C# is a more strict language and is going to trow much more errors where you can see what is wrong.

lol, believe me - I haven’t a clue what’s going on. I originally intended for another friend of mine to do most of the programming. Anyway, thanks for your help i’ll see what I can do.

That’s why i know how to use Blender beyond making cubes and spheres

Yes i’m more into modelling myself (not great at it but it’s my strongest point lol). Anyway, i’ve managed to get it working with help from a friend anyway! I made it on trigger and put it on an empty game object inside my cube and put the script on that. Thanks to all who helped me in this thread