Hi there I am using Unity 5.5.1f1, receiving the error " The type or namespace name ‘Projectile’ could not be found (are you missing a using directive or an assembly reference?" and the console is saying “Projectile' is atype’ but a `variable’ was expected”. The Projectile script is on the lasers, the ships have their rigidbody2d and poly colliders.
Here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBehaviour : MonoBehaviour {
void OnTriggerEnter2D(Collider2D collider)
{
Projectile missile = collider.gameObject.GetComponent<Projectile>();
if (missile) {Debug.Log("Hit by projectile"); }
Any help greatly appreciated
Rob
March 16, 2017, 11:03pm
2
Can you paste up your Projectile script also @Sam_Blank
1 Like
Hi Rob!
its just:
public class Projectile : MonoBehaviour {
public float damage = 100f;
}
Rob
March 16, 2017, 11:04pm
4
No using statement at the top?
1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour {
public float damage = 100f;
}
1 Like
Rob
March 16, 2017, 11:05pm
6
What is the file name of the script, is it spelt the same?
1 Like
Yes its just “Projectile” in Unity
1 Like
Rob
March 16, 2017, 11:08pm
8
Wanna zip up your project and I’ll take a look? (assuming you’re on a PC).
1 Like
This is so odd, it completely refused to work so I just closed everything and reloaded and suddenly it seems to recognize it!
So sorry, no idea why it was behaving like that, everything was saved before the close.
1 Like
Rob
March 16, 2017, 11:09pm
10
lol… Gremllins!
Oh, well, you’ve got it sorted which is the main thing…
btw… this will help to make your code easier to read when posting
Code is frequently shared on the GameDev.tv forum often by members of the community seeking an answer to a problem, or by members providing a solution to a problem. Providing consistent, easy to read code makes it easier for the person you are trying to help, or, for the people you are seeking help from.
Code formatting is essential for readability, please consider your community and take an extra moment to format your code in your post.
Applying code formatting;
On a new line, enter thre…
1 Like
Does the code look correct now? I went in to edit but it seemed to all have the “```” on it.
1 Like
Rob
March 16, 2017, 11:14pm
12
That’s because I popped them in for your earlier
1 Like
I got the exactly same problem, cant fix yet.
1 Like
So I fugure out, just in case anyone else come to this thread, my problem as I had the
Projectile missile = collider.gameObject.GetComponent<Projectile>();
inside another If statement, when I put it in the rood, it worked just fine.
2 Likes