Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fjalla · Nov 24, 2012 at 08:28 PM · errorrigidbody

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);
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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. =)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fjalla · Dec 02, 2012 at 01:44 PM 0
Share

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; } } }

avatar image moonLite · Dec 02, 2012 at 06:23 PM 0
Share

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  {}).

avatar image moonLite · Dec 03, 2012 at 02:46 AM 0
Share
 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`?

avatar image fjalla · Dec 04, 2012 at 12:35 PM 0
Share

ups, I think I posted the wrong script in the comment. Wait a $$anonymous$$ute, I'll fix it.

avatar image fjalla · Dec 04, 2012 at 12:39 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges