My project is not compiling due to error CS0246 and I don’t understand what it wants of me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using ScissorsPaperRock;
namespace ScissorsPaperRock
{
public class AIChoice
{
public UNIT aiChoice; // error CS0246: type or namespace name 'UNIT' could not be found (are you missing a using directive or an assembly reference?)
public void start()
{
System.Random rnd = new System.Random(); // Makes the random class.
int AISelect = rnd.Next(0, 3);
{
if (AISelect == 0)
aiChoice = UNIT.ROCK; // Oddly enough, no errors here so far?!?!
else if (AISelect == 1)
The type is being referenced from another code file, here it is.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScissorsPaperRock;
namespace ScissorsPaperRock
{
public class EnumData
{
public enum UNIT // a (player) and b (AI)
{
ROCK,
PAPER,
SCISSORS
}
It seems like it wants a using directive, so I added the using ScissorsPaperRock; and put them all in the namespace and this (like I expected) did nothing.
What using directive am I missing? Am I missing a using directive? Why isn’t the error showing up for the instance of referencing UNIT? I just don’t understand what the heck is going on?
EDIT: The answer is simple and twofold. You can either make any reference to the enum here be EnumData.UNIT, or you can simply move your enum out of a class and define it at namespace level, this will allow it to simply be referenced with UNIT instead. At least I think so.
Yes, it’s because you defined the enum inside of a class, so the type isn’t just UNIT, it is EnumData.UNIT
Defining the enum outside of the class will fix that.
I have the same problem. I am following a tutorial and am getting the same error code:
error CS0246: (25,12) The type or namespace name “CamShake” Could not be found.
Are you missing a using directive or assembly reference?
This is the code:
using UnityEngine;
using TMPro;
public class GunSystem : MonoBehaviour
{
//Gun stats
public int damage;
public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
//bools
bool shooting, readyToShoot, reloading;
//Reference
public Camera fpsCam;
public Transform attackPoint;
public RaycastHit rayHit;
public LayerMask whatIsEnemy;
//Graphics
public GameObject muzzleFlash, bulletHoleGraphic;
public CamShake camShake;
public float camShakeMagnitude, camShakeDuration;
public TextMeshProUGUI text;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
//SetText
text.SetText(bulletsLeft + " / " + magazineSize);
}
private void MyInput()
{
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Shoot
if (readyToShoot && shooting && !reloading && bulletsLeft > 0){
bulletsShot = bulletsPerTap;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
//Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate Direction with Spread
Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
//RayCast
if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
{
Debug.Log(rayHit.collider.name);
if (rayHit.collider.CompareTag("Enemy"))
rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);
}
//ShakeCamera
camShake.Shake(camShakeDuration, camShakeMagnitude);
//Graphics
Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.Euler(0, 180, 0));
Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot--;
Invoke("ResetShot", timeBetweenShooting);
if(bulletsShot > 0 && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
readyToShoot = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}
Please don’t necro post, especially for compiler errors. You can fix all of those yourself. See below.
Most likely you have a typo or you failed to finish the part of the script that makes a CamShake, or you failed to name it CamShake.
These are just a few possibilities. Here is how you can track it down:
Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly, two (2) simple steps to success:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped. Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE!!
If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
Finally, when you have errors, don’t post here… just go fix your errors! Here’s how:
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.