Object reference not set to an instance of an object

Hey, wenn ich versuche meinen Charakter zum schießen zu bringen (Linksklick) kommt diese Fehlermeldung:

NullReferenceException: Object reference not set to an instance of an object
PersonScript.shoot () (at Assets/BAUWERKE/Scipts/javascript/PersonScript.js:31)
PersonScript.Update () (at Assets/BAUWERKE/Scipts/javascript/PersonScript.js:24)

Ich hoffe es kann mir jemand helfen :slight_smile:


class PersonScriptShooting {

	var projectile : Rigidbody;
	
	public var damage = 5;
	
	private var go : GameObject;
	
	var Spawnpoint : GameObject;	
}
var launchProjectile : PersonScriptShooting = PersonScriptShooting();

// Oben Klassendeklarationen, möglichst viel in ganze Klassen zusammenfassen.
//
// Unten der normale Code.

function Start()
{

}

function Update()
{
	shoot();
}

function shoot()
{
	if(Input.GetButtonDown("Fire1"))
	{
		go = Instantiate(launchProjectile.projectile.gameObject, launchProjectile.Spawnpoint.position, launchProjectile.Spawnpoint.Rotation);
		var RichtungsVektor : Vector3 = Vector3(launchProjectile.Spawnpoint.position.x,launchProjectile.Spawnpoint.position.y,launchProjectile.Spawnpoint.position.z);
		var elevation : Vector3 = Quaternion.Euler(RichtungsVektor) * transform.forward;
		new go.rigidbody.AddForce(elevation * 10);
	}	
}

Hast du Spawnpoint initialisiert? Du hast da ein GameObject namens Spawnpoint deklariert aber das alleine reicht nicht. Da muss dann auch ein GameObject zugewiesen sein. Schau mal ob Spawnpoint im Inspector auftaucht (kenn mich mit JavaScript nicht aus…). Wenn ja, weise dort ein entsprechendes GameObject zu.

in english: Spawnpoint needs to be initialized.

I don’t speak the language here, but… When you use instantiate it will often instantiate a type that is not a GameObject. For instance, in this case it may be that it’s instantiating as a generic Object. Just track through all your codes and see what it could be instantiated as.

You can also use Instantiate(…) as GameObject, to attempt a better typecast.

Problem solved.

class PersonScriptShooting {

	var projectile : GameObject;
	
	var spawnpoint : Transform;
	
	var camera : Transform;
}
var launchProjectile : PersonScriptShooting = PersonScriptShooting();


function Update()
{
	shoot();
}


function shoot()
{
	if(Input.GetButtonDown("Fire1"))
	{
		var spawn : Vector3 = Vector3(launchProjectile.spawnpoint.position.x,launchProjectile.spawnpoint.position.y,launchProjectile.spawnpoint.position.z);
		var clone : GameObject = Instantiate(launchProjectile.projectile,spawn,launchProjectile.camera.rotation);
		clone.rigidbody.AddForce((spawn - Vector3(launchProjectile.camera.position.x,launchProjectile.camera.position.y,launchProjectile.camera.position.z)) * 50, ForceMode.Impulse);
	}	
}

Problem solved:

class PersonScriptShooting {

	var projectile : GameObject;
	
	var spawnpoint : Transform;
	
	var camera : Transform;
}
var launchProjectile : PersonScriptShooting = PersonScriptShooting();


function Update()
{
	shoot();
}


function shoot()
{
	if(Input.GetButtonDown("Fire1"))
	{
		var spawn : Vector3 = Vector3(launchProjectile.spawnpoint.position.x,launchProjectile.spawnpoint.position.y,launchProjectile.spawnpoint.position.z);
		var clone : GameObject = Instantiate(launchProjectile.projectile,spawn,launchProjectile.camera.rotation);
		clone.rigidbody.AddForce((spawn - Vector3(launchProjectile.camera.position.x,launchProjectile.camera.position.y,launchProjectile.camera.position.z)) * 50, ForceMode.Impulse);
	}	
}