I am following a tutorial to familiarize myself with Unity and when I try to compile my script I get this error message:
Error CS0246: The type or namespace name `UnityEngine’ could not be found. Are you missing a using directive or an assembly reference?
It is pointing to line 1 where it simply has using UnityEngine;
It makes no sense that it is pointing to something that is already put there so I must be missing something. I am putting the entire code below in the hopes that someone can tell me what I am missing. I have come across this with every single tutorial I have tried to follow now and am getting tired of not being able to get passed this one issue. Any help would be greatly appreciated.
I am writing in C# and this is a simple script that is going to grow into a cannon that will fire a cannonball but for now is to make the camera move around the scene.
using UnityEngine;
using System.Collections;
public class Cannon : MonoBehaviour {
// Update is called once per frame
void LateUpdate ()
{
float x = Input.GetAxis("Mouse X") * 2;
float y = Input.GetAxis("Mouse Y");
//vertical tilting
float yClampled = transform.eulerAngles.x + y;
transform.rotation = Quaternion.Euler(yClamped, transform.eulerAngles.y, transform.eulerAngles.z);
//horizontal orbiting
transform.RotateAround(new Vector3(0, 3, 0), Vector3.up, x);
}
}
Thanks
I want to thank you all for helping out. I managed to get it all working as it was supposed to. I decided to start with a fresh project (mine was small and didn’t hurt to do so) and I went through the simple operations of Assets-> Create-> C# and named the file. From there I moved the script into the scripts folder and then attached it to the object I wanted it on. Then I opened the script and edited it and then just hit CMD+S and then went back and tested the game. I still got warnings but I was able to trouble shoot those and what I was forgetting to do was put my projectile prefab into the Inspector portion of the script. Once I did that it seemed to run perfectly. So if anyone else out there is running into similar issues then I hope this helps you out also. Again, I want to thank you guys for helping my and I am going to post the final code below just so it is there if anyone wants to make a projectile that shoots from their camera (or whatever) like the tutorial I was following.
Main Camera Script is titled Cannon
using UnityEngine;
using System.Collections;
public class Cannon : MonoBehaviour {
public GameObject projectilePrefab;
void LateUpdate() {
float x = Input.GetAxis("Mouse X") * 2;
float y = -Input.GetAxis("Mouse Y");
// vertical tilting
float yClamped = transform.eulerAngles.x + y;
transform.rotation = Quaternion.Euler(yClamped, transform.eulerAngles.y, transform.eulerAngles.z);
// horizontal orbiting
transform.RotateAround(new Vector3(0, 3, 0), Vector3.up, x);
}
void FixedUpdate () {
if (Input.GetButtonDown("Fire1")) {
GameObject projectile = Instantiate(projectilePrefab, transform.position, transform.rotation) as GameObject;
projectile.rigidbody.AddRelativeForce(new Vector3(0, 200, 500));
}
}
}
Cannonball script is cleverly titled Cannonball
using UnityEngine;
using System.Collections;
public class Cannonball : MonoBehaviour {
public GameObject projectilePrefab;
void FixedUpdate () {
if (Input.GetButtonDown("Fire1")) {
GameObject projectile = Instantiate(projectilePrefab, transform.position, transform.rotation) as GameObject;
projectile.rigidbody.AddRelativeForce(new Vector3(0, 200, 500));
}
}
}
Now I am just messing with the numbers and then going to be making some additions to grow this project into something bigger in order to keep learning.
Thanks