Noob: Unknown identifier problem

Hello, I’m very new to programming, I’ve been taking a crash course in java script since the beginning of summer. It has lead me up to the point in an attempt to make my first game for practice.

So any ways here is the error that unity is giving me:

Assets/Scripts/Scriptplayer.js(26,33): BCE0005: Unknown identifier: ‘myEnemyScript’.

Heres the lines that involve the error in the code:

var myEnemyScipt = hit.transform.GetComponents(ScriptEnemy); 
				myEnemyScript.numberOfClicks -= 1;

NOTE: Line 26 is the bottom one

When I hit the error I was trying to call up a var from another script.

Heres the var from the other script:

var numberOfClicks 	: int = 2;

I will appreciate any help you guys can give :slight_smile: !!

Try:

var myEnemyScipt: ScriptEnemy = hit.transform.GetComponents(ScriptEnemy); 
				myEnemyScript.numberOfClicks -= 1;

Thank you very much Static nova, that did get raid of the error. However now I have a new one.

Which is:

Assets/Scripts/Scriptplayer.js(25,93): BCE0022: Cannot convert ‘UnityEngine.Component[ ]’ to ‘ScriptEnemy’.

Never-mind solved that problem and now at least I can play the game. But, when I click the enemy I get an Null exception. When it is surpose to re spawn after an delay.

Anyways here is the null exception error:

NullReferenceException: Object reference not set to an instance of an object
Scriptplayer.Update () (at Assets/Scripts/Scriptplayer.js:27)

and the new code:

var myEnemyScript: ScriptEnemy = hit.transform.GetComponents(ScriptEnemy) as ScriptEnemy; 

                myEnemyScript.numberOfClicks -= 1;

When I double click the null error it points to this line in specfic.

myEnemyScript.numberOfClicks -= 1;

Your using GetComponents (plural). You want to use GetComponent…

Try doing this in your code
myEnemyScript[0].numberOfClicks -= 1;

I bet it will work because your dealing with an array of ScriptEnemy’s. This is why strictly typing variables is nice.

You misspelled myEnemyScript :stuck_out_tongue:

EDIT: I see you’ve fixed it now.

Thanks, Ereous! but after I tried it I just got another error…(:…

Here it is:

Assets/Scripts/Scriptplayer.js(27,17): BCE0048: Type ‘ScriptEnemy’ does not support slicing.

    //An Array of Enemy Scripts. Since this one object can only have one script we know its at the first location in the index.
    //Which is most times 0 in programming.
   var myEnemyScript: ScriptEnemy[] = hit.transform.GetComponents(ScriptEnemy);
                    myEnemyScript[0].numberOfClicks -= 1;
    //Singular Enemy Script
   var myEnemyScript: ScriptEnemy  = hit.transform.GetComponent(ScriptEnemy);
                    myEnemyScript.numberOfClicks -= 1;

Thanks, but I’m using singular and thats how my code started I had to add an “as ScriptEnemy” at the end for the error to go away on that specific line. At the moment it’s the line below thats causing the problem.

Anyways, I fixed the slicing error and now I’m back at the same NUll error as before.

this time I’m going to show some screenshots:

1296067--59848--$Picture 1.jpg1296067--59849--$Picture 4.png

The specific line:

myEnemyScript.numberOfClicks -= 1;
var myEnemyScript: ScriptEnemy  = hit.transform.GetComponent(ScriptEnemy);

//We don't want to access the script if its null.. Something when wrong when we attempt to grab the reference
if(myEnemyScript != null){
	myEnemyScript.numberOfClicks -= 1;
}

Thanks, this gets riad of the null, but now not of the funcations I wanted to acces from the other script isn’t working I think what needs fixing here is the enemy script.

Heres the code:

//Enemy Script

//Inspector Variables
public var numberofClicks : int = 2;
var respawnWaitTime : float = 2.0;
var explosion       : Transform;
//Private Variables
private var storeClicks : int = 0;

function start ()
{
	storeclicks = numberofClicks;
}

//Update is called every frame
function Update () 
{
	if(numberofClicks <= 0)
	{
		if(explosion)
		{
			Instantiate (explosion, transform.position, transform.rotation);
		}
		
		var position = Vector3 (Random.Range(-6, 6),Random.Range(-4, 4),0);
    	RespawnWaitTime ();
    	transform.position = position;
    	numberofClicks = storeClicks;
		print("Enemy hit!");
	}	
}

function RespawnWaitTime ()
{
	renderer.enabled = false;
	yield WaitForSeconds (respawnWaitTime);
	renderer.enabled = true;
}

There’s nothing wrong with your code. Whatever you’re hitting doesn’t have that component attached to it.

Yeah KelsoMRK is right, probably since you haven’t shown all the code involved.

Whenever you are dealing with “Hitting” anything you need to check if it has the script or is an Enemy entity… etc before actually trying to pull a reference from that Object.

Since your getting the component from Hit object I’m guessing your using a Raycast? You could easily only check for the Enemy layer.

void FireShot()
{
	var enemyLayer : int = 1<<8;
	var fwd = transform.TransformDirection (Vector3.forward);
	var enemyScript : ScriptEnemy = null;
	if (Physics.Raycast (transform.position, fwd, 10, enemyLayer)) {
	    print ("We hit an enemy layer!");
	    enemyScript = hit.gameObject.GameComponent(ScriptEnemy);

	    //Do things with the enemy script
	}
}

Guess what guys, I fixed my problem. It had nothing to do with code, I put the script enemy in the wrong place.

I put the script on “EnemyDroneOrefab”, so I moved the enemy script to “PolySurface8”. the game worked and all is well !!! YAY !!! I can’t believe such a silly stupid mistake would set me back soooo much and for soooo long.

Well thanks to those who helped me, so far this has been a great learning experience. Hopefully I can get Lab 1 finished soon.