- Home /
Problem accessing variable in javascript
Hi everyone, I'm trying to make a sphere (testObject1) change direction when collides with a rigidbody.
The idea is to keep the sphere bouncing from 1 point to another (testObject2 and testObject3). So, I came up with this:
var speed = 0.10;
var dropPoint = 1;
function Update () {
if(dropPoint == 1){
transform.Translate(0, 0, speed);
}else{
transform.Translate(0, 0, speed - (speed * 2));
}
}
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
if(collision.collider == "testObject3"){
dropPoint = 2;
}else{
if(collision.collider == "testObject2"){
dropPoint = 1;
}
}
if(collision.collider != "testFloor"){
print(collision.collider);
}
}
}
The problem is that when the Sphere collides with testObject3, it just keeps going in the same direction and not in the opposite one as it should go.
What happening here? Any idea?
Thanks!
Answer by SilverTabby · Oct 03, 2011 at 11:21 AM
The majority of your problems are in this function:
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
if(collision.collider == "testObject3"){
dropPoint = 2;
}else{
if(collision.collider == "testObject2"){
dropPoint = 1;
}
}
if(collision.collider != "testFloor"){
print(collision.collider);
}
}
I'm going to go line by line and explain what's going on here:
function ...{
This line is done perfectly. Moving on...
foreach( ContactPoint ) {
Huh? Why are you trying to loop through the points? You never use any of them! This loop does nothing. You only need to evaluate the body once
if(collider == "testObject3")
This will never evaluate to true. Why? Because you're comparing a collider with a string. What you're trying to do is
if(collider.**name** == "testObject3")
Now you're comparing a string with a string. This can and will evaluate to true.
There are several repeats of the above problem
if(collision.collider != "testFloor")
Again, this will never evaluate to false. If you always want this to print, then just remove the if in front of print altogether.
The only other thing I can add is that I would change your Update function to this, it looks more professional, and is easier to edit:
PLEASE DO NOT COPY-PASTE THIS! RETYPE IT BY HAND. It seems tedious, but trust me, it helps. You don't need to retype the comments, they exist so that you understand what you are typing.
//this function is called once per frame.
function Update ()
{
//switch-case is shorthand for if(dropPoint==1){}else if(==2){}else if(=="etc"){}else
switch(dropPoint)
{
//if you forget the break, it will do case 1, then do case two, then do case 3...
case 1:
transform.Translate(0, 0, speed * Time.deltaTime); break;
case 2:
transform.Translate(0, 0, -speed * Time.deltaTime); break;
default: /*if none of the above cases are true, do nothing*/ break;
}
//you can have more than one action per case, as long as there is a break; at the end
}
Thank you very much SilverTabby, I can now see my mistakes and start to cleaning them up. As soon as I'm able to do the changes I'll let you know about it.
One other question... is it possible to edit the code within Unity? Because, I'm not able to edit the code in the right panel, only allowed to double click it, and it opens in Notepad. Is there any way to change this? Because it's kind of annoying to use an external editor, an external window just to make a simple change.
Once again, thanks!
Well, if you set it up right it should be using $$anonymous$$onoDevelop (which, if you do a lot of coding, you should have up at all times) which is a proper IDE (much better than notepad!)
Yeah, but isn't there a way to edit in-Unity the code? I mean, to edit something little, is there the possibility to edit from the Inspector itself when you select a javascript file?
Unity really isn't about editing assets in the editor- this applies to code just as much as it does to mesh models. Just like any other asset, script files must be edited from an external application!
In that case, thanks very much for taking the time to explain that to me! Thanks very much!
Your answer
Follow this Question
Related Questions
Javascript Movement Script. Why am I getting these errors? 2 Answers
Call JavaScript from c# Help! 0 Answers
Static Typing for JS function? 2 Answers
Doing something mutiple times in function() Start 1 Answer
Talking Code 2 Answers