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:
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.
//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;
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.
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;
}
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
}
}
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.