Not derving from monobehavior

The OnCollisionStay is not deriving from Monobehavior for some reason and it’s spelled right because I used a code snippet. Other funky things are going weird with not working with unity in my code for some reason (I can’t find FixedUpdate or other functions)?!

Hi,

For which course project is this?

It’s my own project. It’s not from any course.

your screenshot does not show whether or not you have the correct Class definition for example some like:

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

public class PlayerController : MonoBehaviour
{
}

you might be missing the " : MonoBehavior" part or a “using” reference or Visual Studio Code needs to be updated and/or restarted. I am not sure how Visual Studio Code works but if it works like Visual Studio 2019 Community Edition then those are the possibilities that I see could be the solution to your problem.

Yah, It looks the same as you posted in your code. I’ll try and restart.

using UnityEngine;

public class PlayerController : MonoBehaviour {
    
    [SerializeField]private Rigidbody rb;
    [SerializeField]private Collider col;

    private float speed;
    private Vector3 inputVector;
    private float inputX;
    private float inputY;
    private bool isGrounded;

    void Awake()
    {
        speed = 10;
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {

        if(Mathf.Abs(Input.GetAxis("Horizontal")) > 0 || Mathf.Abs(Input.GetAxis("Vertical")) > 0)
        {
            inputVector = new Vector3(Input.GetAxis("Horizontal") * speed, rb.velocity.y, Input.GetAxis("Vertical") * speed);

            transform.LookAt(transform.position + new Vector3(inputVector.x, 0, inputVector.z));

            rb.velocity = inputVector;
        }

        if(Input.GetButtonDown("space") || isGrounded)
        {

        }

        
    }

    void OnCollisionStay(Collision other)
      {
              isGrounded = true;
      }
}

I remember reading about how some people had trouble with their Visual Studio Code Unity plugin and a bug was found that was fixed in the latest version of the plugin. The latest version of the Visual Studio Code plugin is version 1.1.2. And the latest version of Unity is version 2019.2.4f1. Otherwise your code looks good. I was even able to copy your script and test in a new project with a simple cube and plane object using the latest version of Unity and Visual Studio (although I did have to comment out the “if(Input.GetButtonDown(“space”) || isGrounded)” method to get it to work).

1 Like

I tried restarting VSCode but that didn’t work, but thank you anyway! I am using unity 2019.3 actually, which is the beta version of unity currently with the new UI and other small features.

Last time I had this deleting the Library folder fixed it.

Which library are you talking about? I tried deleting the main library for my project and starting unity back up again but it didn’t change anything. It still says PlayerController.OnCollisonEnter when I hover over the code. It should be saying Monobehavior.OnCollisonEnter, right?

Thats odd. I don’t know what it would be then.

Given there are Unity Tools for VS Code, did you install them? Is VS Code selected as your External Script Editor in Unity (Edit > Preferences > External Tools)?

Also please feel free to ask our helpful community of students for advice in our official Discord chat.

Since your PlayerContoller script derives from Monobehavior, Visual Studio is correct in saying that the OnCollisonEnter method you have in your code is part of the PlayerController class. Meaning the statement “PlayerController.OnCollisonEnter” means that you want to access the PlayerController class’s OnCollisonEnter function which is further defined by the Monobehavior class since you have the statement at top “PlayerController : Monobehaviour”. If you didn’t have the “PlayerController : Monobehaviour” (for example, you just had “PlayerController” at the top) statement then you would need to reference the Monobehaviour class explicitly. Here is an article I found which talks about C# classes and class inheritance: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes. I hope this explanation helps.

1 Like

This topic was automatically closed after 5 days. New replies are no longer allowed.

Privacy & Terms