Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 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 1-Jul-20 3:47am
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 1-Jul-20 14:08pm    
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.

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

Change the "gameObject" your have to "GameObject" and the code might compile.
 
Share this answer
 
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.

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.
 
Share this answer
 
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:
C#
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.
 
Share this answer
 
v2
Comments
Dave Kreskowiak 1-Jul-20 10:10am    
GameObject is a standard class in the Unity library.
Richard MacCutchan 1-Jul-20 10:33am    
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 1-Jul-20 11:20am    
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900