Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetComponent<Rigidbody>() to addForce on another gameobject causes errors in javascript

Discussion in 'Scripting' started by thePaintBrush, Apr 14, 2016.

  1. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Hello, I've been trying to get a trigger to make a object move up when the player collides with a another object that triggers that movement. This is done in unity 5.3.3

    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider)
    2. {
    3.     if (other.tag == "Player")
    4.     {
    5.         moveCube.getComponent<Rigidbody>()transform.addForce(Vector3(0, 2, 0));
    6.         return;
    7.     }
    8. }
    Problem is I get the following errors.

    How can this be solved with one script with both Enter and Exit trigger functions?

    Thanks.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Generic arguments in UnityScript need more syntax.
    moveCube.GetComponent.<Rigidbody>().AddForce(...)

    Notice the extra period. Also notice it's GetComponent, not getComponent. And it's AddForce not addForce. And you've got some weird extra transform reference slapped in the middle of the statement. And the return statement there isn't doing anything if that's the entirety of your OnTriggerEnter method.
     
    josephzli2011 likes this.
  3. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    I see. Implementing your method, what about Unknown identifier errors for both OnTriggerEnter&Exit functions.

    This is a problem because I thought that way the game object when the name of the object is added, that it would be error free. Because this is only suppose to move a cube up when entering and go down when exiting the trigger.
     
    Last edited: Apr 15, 2016
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    for the most part collision functions either do something to the current gameobject, or the thing being collided with (accessed via the "other" parameter). If "moveCube" is something else you'll need to setup a reference in your script.
     
  5. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Reference besides adding a var on top of the script?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You'll have to post more code to get a helpful response.
     
  7. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Basically theirs a cube that acts as a trigger. Here's the script attached to it.

    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider)
    2. {
    3.     if (other.tag == "Player")
    4.     {
    5.         moveCube.GetComponent.<Rigidbody>().AddForce(Vector3(0, 2, 0));
    6.     }
    7. }
    8.  
    9. function OnTriggerExit(other: Collider)
    10. {
    11.      if (other.tag == "Player")
    12.      {
    13.          moveCube.GetComponent.<Rigidbody>().AddForce(Vector3(0, -2, 0));
    14.      }
    15. }
    Again, this trigger is suppose to add force/move a separate cube(moveCube) without it requiring a an additional script attached. Recap...

    1. Theirs a trigger with the script above. Therefore only one script is needed in this case to keep things simple.

    2. This script is suppose to make another cube add force.

    3. That cube(moveCube) moves without an additional script because the trigger makes it move when they both have rigidbodies.

    I thought about adding a var on top of the script but doesn't work. And I don't understand how I could add Enter And Exit trigger functions into one whole function to make the script look more cleaner and less sloppy.

    What about adding a function update to replace OnTriggerExit?
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Is that your entire script? If so you never declare moveCube anywhere.
     
  9. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    As a var on top? How would that be done?
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  11. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Another thing I thought of was add force in the context of the 2d jumping platformer like from Unity's basic platformer project even though it's in the context of c#.

    Code (CSharp):
    1.  
    2. void FixedUpdate()
    3. {
    4.        float h = Input.GetAxis("Horizontal");
    5.  
    6.        anim.SetFloat("Speed", Mathf.Abs(h));
    7.  
    8.         if (h * rb2d.velocity.x < maxSpeed)
    9.              rb2d.AddForce(Vector2.right * h * moveForce);
    10.  
    11.         if Mathf.Abs(rb2d.velocity.x) > maxSpeed)
    12.              rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
    13.  
    14.         if (h > 0 && !facingRight)
    15.              Flip ();
    16.         else if (h < 0 && facingRight)
    17.              Flip ();
    18.  
    19.          if (jump)
    20.          {
    21.              anim.SetTrigger("Jump");
    22.              rb2d.AddForce(new Vector2(0f, jumpForce));
    23.              jump = false;
    24.          }
    25. }
    26.  
    Assuming this is inside of a public class. What would that mean for using a vector3 variable. Like for a OnTriggerEvent in a void.

    Code (CSharp):
    1.  
    2.         void OnTriggerEnter()
    3.         {
    4.                rigidbody.Addforce(-transform.up * 6);
    5.                rigidbody.useGravity = true;
    6.         }
    7. }
    8.  
    This doesn't work in unity 5.
     
    Last edited: Apr 26, 2016
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I don't understand your question.
     
  13. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Assuming that the first chuck of code was a method of using AddForce to make the player jump, how would that work if the player collides with a OnTriggerEnter function on another object, causing that object/prefab to jump?

    Does that sound clear enough? Or convoluted to you?

    For the second chuck of code rigidbody.Addforce(-transform.up * 6); does not work in unity 5. All I'm trying to do is make the player collide with a object causing it to jump with unityscript.
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    "does not work" isn't enough information to provide a helpful response. Have you ensured that the trigger is actually working? Is that your actual code because OnTriggerEnter takes a Collider argument. What does the rigidbody do when the trigger is entered? What are you expecting to happen? It's also confusing when you post completely unrelated code and essentially ask - well if this works why doesn't this other thing work? Also, if you're writing in UnityScript why is everything you're posting in C#?
     
  15. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Problem is when I was looking at tutorials that implemented AddForce for jumping on this website, it was only for c#. I was assuming that there would be users on here that could explain how something that should be simple like making another object jump with a trigger event would work.

    Again, with the second AddForce example when I tried it with both c# and unityscript scripts, I don't understand how that could work with unity 5 when it's not supported.
     
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Are you talking about using the rigidbody shortcut property? If so just replace it with GetComponent<Rigidbody>() or cache it in Awake. And again - "doesn't work" is not an adequate explanation of the issue.
     
  17. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Even with a prefab cube that is a rigidbody attached to the trigger? This is a concern I have because there has to be a trigger for the prefab and if a trigger is enabled for the box collider that is a parent to the cube itself, wouldn't that mean it would fall through the level?

    Using both lines of code in this video aren't supported with unity 5.


    This is what I don't understand. If both lines of code are used to make an object move in this video, with a mouse click, how come it's not supported with Unity 5's physics system? Especially when I want to pull this off with unityscript code.
     
  18. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That has absolutely nothing to do with what I said.

    This shouldn't be this hard.
    What do you mean when you say "aren't supported"?
     
  19. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Using AddForce function in the context of rigidbody.AddForce and making useGravity equal true like in the video above.

    When I use unity 5 I get the following errors.

    I say using both lines this way aren't supported in this version of unity 5 because this only works for older pre 5 versions of unity.
     
  20. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Ok - so it is what I said.

    From before:
    In 5 they deprecated those shortcut properties and changed their return types to be Component.
     
  21. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    So that would mean Get would be removed from Component?

    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider)
    2. {
    3.     if (other.tag == "Player")
    4.     {
    5.         moveCube.Component.<Rigidbody>().AddForce(Vector3(0, 2, 0));
    6.     }
    7. }
    8.  
     
  22. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  23. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    I must of misinterpreted what you said previously. Would you say the right way to deal with Add Force function would be work this way?

    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider)
    2. {
    3.         if (other.tag == "Player")
    4.         {
    5.              moveCube.GetComponent<Rigidbody>().Addforce = Vector3.up 2f;
    6.         }
    7. }
    8.  
     
  24. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No - AddForce is a method. You've written it correctly before, why are you changing it now?
     
  25. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    I think the thing to do is take a step back and read up on the fundamentals of C# (or UnityScript, I just find C# to be better supported personally) syntax. Because right now you're spending more time and energy on stumbling around it than it would take to learn it.

    You can go a ways with examples, but ultimately they're not going to help unless you comprehend them. By which I mean - you should be able to look at a line in the code and know what purpose every character in that line serves.
     
    Last edited: Apr 27, 2016
    Kiwasi likes this.
  26. thePaintBrush

    thePaintBrush

    Joined:
    Oct 10, 2012
    Posts:
    33
    Correctly like as the previous example with a GetComponent? If so, I still get a error about Unexpected token.
     
  27. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Go do the tutorials in the Learn section.
     
  28. josephzli2011

    josephzli2011

    Joined:
    Jul 1, 2020
    Posts:
    4
    Thank you so much!