Unity 5 Not recognizing what "Projectile"?

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 :slight_smile:

Can you paste up your Projectile script also @Sam_Blank :slight_smile:

1 Like

Hi Rob!

its just:

public class Projectile : MonoBehaviour {

    public float damage = 100f;
}

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

What is the file name of the script, is it spelt the same?

1 Like

Yes its just “Projectile” in Unity

1 Like

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

lol… Gremllins! :slight_smile:

Oh, well, you’ve got it sorted which is the main thing… :slight_smile:

btw… this will help to make your code easier to read when posting :slight_smile:

:slight_smile:

1 Like

Does the code look correct now? I went in to edit but it seemed to all have the “```” on it.

1 Like

That’s because I popped them in for your earlier :wink:

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

Privacy & Terms