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

Asking for a help!

Discussion in 'Scripting' started by Tauras, Jul 28, 2015.

  1. Tauras

    Tauras

    Joined:
    Jul 28, 2015
    Posts:
    13
    Hey folks! I'm a beginner in Unity (never used it before). I've started a new project today, and wanted to ask you guys what am I doing wrong?

    This is the script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public float speed = 50f;
    7.     public float jumpPower = 150f;
    8.  
    9.     public bool grounded;
    10.  
    11.     private Rigidbody2D rb2d;
    12.  
    13.     void Start ()
    14.     {
    15.     rb2d = GameObject.GetComponent<Rigidbody2D>();
    16. }
    17.  
    18.  
    19.     void Update ()
    20.     {
    21.  
    22. }
    23.  
    24.     void FixedUpdate()
    25.     {
    26.  
    27.     float h = Input.GetAxis ("Horizontal");
    28.  
    29.     rb2d.AddForce((Vector2.right * speed) * h);
    30.    
    31.     }
    32. }
    I've tried to create a 2D game, and followed instructions of this video:
    . That guy has been using Microsoft Visual Studio Assemble C-SHARP. So my question is: Is there a difference between what program do I use to script? (I'm using Assembly CSHARP, but it is developed by Unity), and maybe you can tell me if I'm writing something wrongly?

    Sorry for creating a thread (possibly in wrong place), but couldn't find a place where I can ask help.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You can use any code editor you want to write the scripts, so that's not the issue.

    If there's an issue with your code, you'll have to tell us more about it.
     
    Tauras likes this.
  3. Tauras

    Tauras

    Joined:
    Jul 28, 2015
    Posts:
    13
    I keep getting an error : "
    Assets/Scripts/Player.cs(15,35): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)'
    "
    Something wrong is with 15th line :c
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    No reason for the GameObject infront of the GetComponent. It's not a static method.

    Code (csharp):
    1.  
    2. rb2d = GetComponent<Rigidbody2D>();
    3.  
     
    Tauras likes this.
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    It would also work if it were `gameObject` instead of `GameObject`. Might have just been a typo.

    GameObject refers to the class of GameObject while `gameObject` is a shortcut for "the GameObject this script is attached to". A further implicit shortcut (via @GroZZleR ) is not even specifying gameObject and just typing GetComponent directly and Unity understands that you are looking on this GameObject.
     
    Tauras likes this.
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    That's sort of shorthand though- GetComponent is used on any specific GameObject in your game to find components (scripts) that are attached to it. You need a GameObject reference (a specific GameObject) in order to use it. When you say "GameObject.GetComponent" you're trying to use it on the class "GameObject" and not on an actual, real object that exists in the scene, and so it tells you "not a static method". A static method is one that exists for the class as a whole, rather than a specific instance (real object) made from it.

    @GroZZleR wrote the way to get a component of the CURRENT GameObject, the one that this script it attached to. You can call it without specifying the specific GameObject it's referring to because "this." is implicit. Writing it out the long way, it's basically:
    Code (csharp):
    1. rb2d = this.gameObject.GetComponent<Rigidbody2D>();
    Notice the capitalization of "gameObject", as this refers to the current GameObject and not the GameObject class. Make sense?
     
    Tauras likes this.
  7. Tauras

    Tauras

    Joined:
    Jul 28, 2015
    Posts:
    13
    Thank you for your answers. It is working well right now :) I really appreciate your help! ;)