Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I can't call a function that I made previously

Discussion in 'Scripting' started by clcriado0, Sep 15, 2019.

  1. clcriado0

    clcriado0

    Joined:
    Oct 1, 2018
    Posts:
    2
    Hello, I created the function rb in the start, but when i want to call it using Player1.rb. appears an error (Player1 is a gameObject that I declared it before).

    What can I do? Sorry for my english
     

    Attached Files:

  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    At the top, you set "rb" to be the Rigidbody that is attatched to the Gameobject that this script is also attatched to, Not one on the player1 gameobject

    If rb needs to be the rigidbody on Player1, you need to first find Player1, Then do Player1.GetComponent<Rigidbody>(), to get the rigidbody from that object
     
    clcriado0 likes this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    As long as the rigidbody is on the same gameObject as this script, then your FixedUpdate code should use "this.rb" instead of "Player1.rb".
     
    clcriado0 likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Player1.rb isn't going to work anyways. Player1 is a GameObject and has no clue what rb is. As mentioned, you need to get the Rigidbody component from the Player1 GameObject or you need to get the script that is attached to the Player1 gameobject that has a reference to the Rigidbody component as variable rb.
     
    clcriado0 likes this.
  5. clcriado0

    clcriado0

    Joined:
    Oct 1, 2018
    Posts:
    2
    Thanks to all, like for you all
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    First and foremost: welcome :)

    I just wanted to quickly add that you can use code tags to directly post properly formatted code with syntax highlighting. It's the <> button in the texteditor here on the forum, and also gets explained in the first sticky post of this subforum if you need to read up on it.

    Have fun with Unity!