Click here to Skip to main content
14,576,820 members
Rate this:
Please Sign up or sign in to vote.
See more:
I got an error in Unity and I'm not sure how to fix it can someone help me this is the error:
Quote:
error CS0246: The type or namespace name 'gameObject' could not be found (are you missing a using directive or an assembly reference?)
I am new to Unity and coding so I have no idea what this really means. my code is not done, here it is:

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

public class PlayerCameraController : MonoBehaviour
{
    [SerializeField] private float lookSensitivity;
    [SerializeField] private float smoothing;

    private gameObject player;
    private Vector2 smoothedVelocity;
    private Vector2 currentLookingPos;

    private void Start()
    {
        player = transform.parent.gameObject;
    }

    private void Update()
    {
        RotateCamera();
    }

    private void RotateCamera()
    {
        Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), GetAxisRaw("Mouse Y"));

        inputValues = Vector2.Scale(inputValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));
        smoothedVelocity.x = Mathf.Lerp(smoothing.x, inputValues.x, 1f / smoothing);
        smoothedVelocity.y = Mathf.Lerp(smoothing.y, inputValues.y, 1f / smoothing);

        currentLookingPos + smoothing;
    }

    


}


What I have tried:

I have tried looking for other references to "gameObject" but I can't find any apart from the two that I'm pretty sure are spelled right and have the right capitalization. I have tried changing "gameObject" to "GameObject" and I looked at the top of my screen and there was nothing missing, I am very confused.
Posted
Updated 5 days ago
v2
Comments
RedDk 30-Jun-20 18:14pm
   
This is a generic compiler error (CS0246) in C# and an app such as Visual Studio will report the very error you posted. In VS there is a way to reference the .NET assembly. Perhaps the project file that you opened in order to begin your development was referencing a previous assembly. So check in the reference browser to see that Netframework 4.0 or better is assembly to which the reference points. Typically, (it's been a while ...) a reference set to the assembly that doesn't contain a required method/function/etc will have a yellow YIELD sign next to it in the treeview.
Max Qsack 30-Jun-20 18:34pm
   
Hi, I am sorry for the inconvenience, but I do not really understand what you're trying to tell me.
RedDk 5 days ago
   
And here's another idea. A trick you can add to debugging strategy bag. Using the Find-In-Files do a search for the troubled term. In this case, you'd be running through the project looking for "gameObject". Obviously, or perhaps not so obviously, the CASE option would have to be ticked.

End up using whatever the author of the project used.
Rate this:
Please Sign up or sign in to vote.

Solution 1

C# is case sensitive. gameObject is not the same as GameObject.

Change the "gameObject" your have to "GameObject" and the code might compile.
   
Comments
Max Qsack 30-Jun-20 19:01pm
   
I tried that and it just made more compiler errors. I am following a tutorial and in his, the first game object is "GameObject" but the second one is "gameObject" so Idk what to do
Dave Kreskowiak 30-Jun-20 19:23pm
   
Change
private gameObject player;

to
private GameObject player;


I also get the feeling that this line
player = transform.parent.gameObject;

is wrong. I would compare this line to what's in the tutorial.

Remember, THE CASE OF EVERY CHARACTER MATTERS.
Max Qsack 30-Jun-20 19:33pm
   
hey, I just changed "private gameObject player;" to "private GameObject player;" and I compared the lines and they were identical do Idk what to do. here is the tutorial link: https://www.youtube.com/watch?v=CySBfJciojY&t=430s
Dave Kreskowiak 30-Jun-20 20:25pm
   
Yeah, stay away form the YouTube "tutorials". I haven't seen a good one yet.

Rate this:
Please Sign up or sign in to vote.

Solution 2

There are lots of differences between your code and the code in the video so go back and follow it more carefully. Unity has a massive learning curve and trying to learn Unity and c# at the same time is doubly difficult. Copying code blind from a video without understanding what it does isn't really going to teach you c#. You should try and get a better understanding of c# before trying anything advanced like Unity.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Looking at the video the player object is an instance of the GameObject class, so it must be spelled with a capital 'G'. The other reference is a property of transform.parent and is spelled with a lower case 'g', as follows:
private GameObject player;  // player is an instance of the class "GameObject"

private void Start()
{
    player = transform.parent.gameObject; // player is initialised from the parent.
}

You need to check the tutorial again to see where the GameObject class is defined.
   
v2
Comments
Dave Kreskowiak 5 days ago
   
GameObject is a standard class in the Unity library.
Richard MacCutchan 5 days ago
   
Thanks. No idea why he still got errors after spelling it right. I did watch a bit of the tutorial and it sounded quite well put together; but of course that does not guarantee quality.
Dave Kreskowiak 5 days ago
   
Not to mention that you could be using a newer/older version of Unity than the tutorial, and have to deal with any breaking changes that won't be mentioned in the tutorial.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100