- Home /
Error in script
Can anyone help? It won't turn off the rigidbody gravity when IntriggerX is true.
var InTriggerY : boolean;
var InTriggerX : boolean;
// Changes gravity based on booleans.
function FixedUpdate()
{
if(InTriggerY) {
GetComponent("Rigidbody").useGravity = false;
rigidbody.AddForce (Vector3.up * 10);
} else {
GetComponent("Rigidbody").useGravity = true;
rigidbody.AddForce (Vector3.up * 0);
}
if(InTriggerX) {
GetComponent("Rigidbody").useGravity = false;
rigidbody.AddForce (Vector3.right * 10);
} else {
GetComponent("Rigidbody").useGravity = true;
rigidbody.AddForce (Vector3.right * 0);
}
}
Answer by moonLite · Nov 29, 2012 at 12:51 AM
Hi Doctor Jellyface,
I'm still learning unity. So if my answer isn't good please pardon me.
Before anything else, I assume the above script is attached to the object itself, so the object's `useGravity`; will turn off & on & `AddForce Vector 3` will * 10. (Depending on the `IntriggerX` & `IntriggerY`)
Replace this:
GetComponent("Rigidbody").useGravity = false;
To this : (just use this unless you're getting other objects to turn off their own useGravity)
rigidbody.useGravity = false;
Or this: (base on your coding, but don't use it unless you getting other object's component)
GetComponent(Rigidbody).useGravity = false;
You only use `GetComponent` if you wish to get other game object's scripts or component like rigidbody. So if it's own rigidbody, you can no need to put GetComponent.
Error reason:
And if GetComponent"Rigidbody" (with the " " Quotation marks in GetComponent), it means it's getting Component from a different script language like from Unity javascript to C#.
But there's no other language script involved hence, it has the error.
Reference:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html (see the bottom part)
Please let me know how does it work for you. =)
Tried fixing it like you said, but still the same, even after I put InTriggerX in front of InTriggerY, InTriggerY worked, but InTriggerX didn't. Then I gave up trying to make the rigidbody gravity work and just put constant force there. It works now. Probably just a bug.
Here's the new script:
var TriggerY : boolean; var TriggerX : boolean;
function OnTriggerEnter (other : Collider) { if (other.tag == "AggableRigidbody") { if(TriggerY) { other.GetComponent("AggRigidbodyChangeGravity").normalGravity = false; other.GetComponent("AggRigidbodyChangeGravity").inTriggerY = true; } if(TriggerX) { other.GetComponent("AggRigidbodyChangeGravity").normalGravity = false; other.GetComponent("AggRigidbodyChangeGravity").inTriggerX = true; } } }
function OnTriggerExit (other : Collider) { if (other.tag == "AggableRigidbody") { if(TriggerY) { other.GetComponent("AggRigidbodyChangeGravity").normalGravity = true; other.GetComponent("AggRigidbodyChangeGravity").inTriggerY = false; } if(TriggerX) { other.GetComponent("AggRigidbodyChangeGravity").normalGravity = true; other.GetComponent("AggRigidbodyChangeGravity").inTriggerX = false; } } }
Hi Doctor Jellyface,
1) For your 2 scripts, are they both in unity javascript or one is in C#?
Btw, could you please kindly format your code by typing < pre > coding
< code > or < code> coding
< /code> (without the spacing in between the < >)
So it's easier for others to look at next time.
like this:
var TriggerX : boolean;
function Start () {
}
And also your Syntax Errors (missing curly brackets {}).
GetComponent("Rigidbody").useGravity = false; // (a)
rigidbody.AddForce (Vector3.right * 10); // (b)
For the (a) `useGravity`, I know it's getting another object to turn off its `useGravity`.
For (b) `rigidbody.addforce`, is it apply to its own or it's also getting other objects to turn off its own `useGravity`?
ups, I think I posted the wrong script in the comment. Wait a $$anonymous$$ute, I'll fix it.
Here's the right script.
var normalGravity : boolean = true; var inTriggerY : boolean; var inTriggerX : boolean;
// Changes gravity based on booleans.
function FixedUpdate() {
if(normalGravity)
rigidbody.AddForce (Vector3.down 10); if(inTriggerY == true) rigidbody.AddForce (Vector3.up 10);
if(inTriggerX == true)
rigidbody.AddForce (Vector3.right * 10);
}
The script I posted earlier was the script a attached to the trigger. I fixed that one too.
Your answer
Follow this Question
Related Questions
One way ignoring collision 2 Answers
RigidBodys and Scripts equals Error. 2 Answers
Cannot fix error anyone have any suggestions 1 Answer
Error BCE0018 0 Answers
CAN SOMEONE FINALLY HELP ME! 3 Answers