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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Assets\PlayerStuff.cs(5,42): error CS1031: Type expected (i don't know what to do!)

Discussion in 'Editor & General Support' started by SUPERVEGETA111, Nov 21, 2019.

  1. SUPERVEGETA111

    SUPERVEGETA111

    Joined:
    Oct 7, 2018
    Posts:
    2
    so, i was coding my super smash bros type game when i got this error after making player movement script

    here's the script

    start script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerStuff : MonoBehaviour,


    {
    void Update()
    {
    if ( Input.GetKey("d") )
    {
    Rigidbody.AddForce(100 * Time.deltaTime, 0, 0);
    }

    if ( Input.GetKey("a") )
    {
    Rigidbody.AddForce(-100 * Time.deltaTime, 0, 0);
    }

    }
    }

    end script

    please help! i need to finish this game for an assignment :/
     
  2. VS-Productions

    VS-Productions

    Joined:
    Feb 19, 2017
    Posts:
    7
    Remove the "," after MonoBehaviour (Line 5) - that should be it. :)

    Next time you can try to debug it by yourself:
    "Assets\PlayerStuff.cs(5,42)" means that there is a problem in the fifth line at character 42.
    "Type expected" occurs because the "," after MonoBehaviour indicates that you want to implement an internface to your script (https://learn.unity.com/tutorial/interfaces)


     
    Last edited: Nov 21, 2019
  3. SUPERVEGETA111

    SUPERVEGETA111

    Joined:
    Oct 7, 2018
    Posts:
    2
    First off, i did debug it myself for about 6 hours, second off i need the "," there or else i get this error: Assets\PlayerStuff.cs(13,3): error CS0120: An object reference is required for the non-static field, method, or property 'Rigidbody.AddForce(float, float, float)'

    and this error: Assets\PlayerStuff.cs(13,3): error CS0120: An object reference is required for the non-static field, method, or property 'Rigidbody.AddForce(float, float, float)'

    please help
     
  4. VS-Productions

    VS-Productions

    Joined:
    Feb 19, 2017
    Posts:
    7
    You don't need the ",". If you remove it, your "real" error shows up which is on line 13 of your code.

    You have to reference the Rigidbody which means that you have to assign a specific Rigidbody to your script. I suppose that you put your script on an object with a Rigidbody. You can simply reference it by using

    GetComponent<Rigidbody>().AddForce(100 * Time.deltaTime, 0, 0);

    Let me know if you still have trouble.