Calling another script's method

I know the sound effects issue is going to be addressed in few videos from where i am, but this question of mine might not apply just for sound effects.

I added the enemy’s destruction sound effect to the enemyformation prefab, rather than the enemy unit prefab, and i’m trying to call the method OneLess() that is in the enemyformation.cs file from the alien.cs.

My main difficulty with C# so far has been around exactly that: dealing with and addressing objects.

so i tried this, in alien.cs

 void handleHits(){
	enemyformation = this.GetComponent<enemyFormation>();
(....)
        enemyformation.OneLess();

i’ve been getting an error message saying that such variable doesn’t exist in this context

Do you have a component on your object called enemyFormation? When you say “this”, you are saying that the component you are looking for is attached to the object that the alien.cs is a component of.

No, it’s not a component!
enemyFormation is actually the object that spawns the enemy unit

So I am trying to figure out exactly what you are trying to do. I know you are saying you are trying to call OneLess() from the enemyFormation.cs from alien.cs, I do get that. Did you declare it at the top and call it in the start function? For instance:

public EnemyFormation enemyFormation;

void Start () {
     enemyFormation = GameObject.FindObjectOfType<enemyformation>();
}

void HandleHits () {
    enemyFormation.OneLess();
}

I am just taking a shot in the dark here. I do not know exactly how your scripts and game objects are set up, so the above is just an example.

the way you wrote it seems reasonable.

i’m getting this error message now, though (i copied exactly how you did it)

Assets/Entities/Enemies/alien1.cs(10,16): error CS0246: The type or namespace name `EnemyFormation’ could not be found. Are you missing a using directive or an assembly reference?

Ok 2 things on that… first, make sure you spelled it just like the object. Remember, code is very case sensitive. If you spelled it in all lower case, but call it with capital letters, won’t work. Second, make sure for a fact that enemyformation is actually in your scene

Actually, EnemyFormation is on the scene, but alien is not. it is spawned as the scene starts. Maybe that’s the problem?

No, I don’t believe so. I am assuming that your alien.cs script is tied to your ship prefabs that spawn in the enemy formation?

Just one thing …

Use Capital letter for Class names (and the filename). Eg EnemyFormation. This will help distinguish from a variable which you’d start lowercase, eg enemyFormation

It might even be required…i’m not sure - but that’s the coding convention. My guess though is that’s why it can’t find it.

So define class in EnemyFormation.cs and then use

enemyFormation = GameObject.FindObjectOfType<EnemyFormation>();

Even if you don’t follow the conventions, I believe the filename and class name need to be the same case