- Home /
NullReferenceException problem cant find where
Hi everyone!
Im going crazy with this script. My goal is to get a script that, when I press and hold the mouse button, it charges an IceBall (incrementing it's size with transform.localScale), and when the mouse button is released, it shots the ball. Im trying to comunitate with the Stats script, wich its attached to the player, but I constantly get the NullReferenceException problem. Here are the codes:
EsferaHielo.js:
var cannonball : Transform;
var speed : float = 50; // speed is power per second
var aumento : float = 0.0001; // multiplicador de aumento de tamaño
var powermax : float = 500;
var powermin : float = 100;
private var projectile;
private var nivelHielo = Estadisticas.lvl_hielo;
var tamanio = nivelHielo * 1.5;
var maxTamanio = Vector3(tamanio, tamanio, tamanio);
//function Start(){
//}
function Update()
{
if (Input.GetButtonDown("Fire1"))
projectile = Instantiate(cannonball,
transform.position,
transform.rotation);
StartCoroutine("Cook");
}
// This is a co-routine.
// I used the term "to cook a grenade" as popular in many action games.
function Cook()
{
var power : float = powermin;
while (Input.GetButton("Fire1"))
{
var move = speed * Time.deltaTime;
power = Mathf.MoveTowards(power, powermax, move);
var size: float = aumento * nivelHielo;
projectile.localScale += Vector3(size, size, size);
}
// if (projectile.localScale > maxTamanio)
// {
// projectile.localScale = maxTamanio;
// }
Shoot(power);
}
function Shoot(power : float)
{
projectile.rigidbody.AddForce(transform.forward * power);
Physics.IgnoreCollision(projectile.collider, collider);
}
Estadisticas.js
//#pragma strict
static var vida : int = 100;
static var maxVida : int = 100;
var g_vida : GUIText;
var g_NombreYNobleza : GUIText;
var g_niveles : GUIText;
static var nivel_pj : int = 1;
static var lvl_hielo : int = 1;
static var lvl_fuego : int = 1;
static var lvl_levitar : int = 1;
static var lvl_volar : int = 1;
var nombre = 'Polomeo';
var tituloNobleza = 'Aprendiz';
var experienciaTotal : int = 0; // cada vez que suma XP a un hechizo, tambien se suma aca.
var nivel : int = 0;
private var xp_hielo : int = 0;
private var maxXp_hielo : int = 100;
private var xp_fuego : int = 0;
private var maxXp_fuego : int = 100;
private var xp_levitar : int = 0;
private var maxXp_levitar : int = 100;
private var xp_volar : int = 0;
private var maxXp_volar : int = 100;
private var experienciaSigLvl = 400;
private var titulosDeNobleza = ['Aprendiz', 'Duque', 'Lord', 'Master', 'Capo'];
function Start(){
RegenerarVida();
//RegenerarMana();
}
function Update(){
g_niveles.text = "XP TOTAL =" + experienciaTotal + " - Hielo: " + xp_hielo + " Lvl Hielo: " + lvl_hielo;
g_vida.text = "Vida: " + vida + " / " + maxVida;
g_NombreYNobleza.text = tituloNobleza + " " + nombre + " Nivel " + nivel_pj;
if( vida > maxVida)
{
vida = maxVida;
}
if( vida < 0)
{
Debug.Log('Muerto');
vida = 0;
}
if (Input.GetKeyDown('r'))
{
vida -= 20;
}
if (experienciaTotal > experienciaSigLvl)
{
SubirNivel();
}
if (Input.GetKeyDown('e'))
{
xp_hielo += 10;
experienciaTotal += 10;
}
if (xp_hielo >= maxXp_hielo)
{
SubirNivelHielo();
}
}
function RegenerarVida(){
for(i=1;i>0;i++)
{
yield WaitForSeconds(0.5); // lo que espera es lo que tarda en subir un punto la vida. A mas nivel, deberia subir mas rapido
if(vida < maxVida)
{
vida++;
}
}
}
function SubirNivel(){
experienciaSigLvl += 400 * nivel_pj;
nivel_pj++;
tituloNobleza = titulosDeNobleza[nivel_pj];
maxVida += 50 * nivel_pj;
}
function SubirNivelHielo(){
xp_hielo = 0;
lvl_hielo++;
maxXp_hielo += 50 * lvl_hielo;
}
Thanks!
Yes, that. It will tell you the line number. NullReferenceException means the thing you are trying to use doesn't exist. For example rigidbody.AddForce if you forgot to add a rigidbody.
FYI: cooking a grenade means holding it live in your hands for a second or two -- "cook off" some of the time -- often to prevent it being tossed back. Seems like something the player would do.
Sorry, this is the error it gives me:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
EsferaHielo.Shoot (Single power) (at Assets/EsferaHielo.js:52)
EsferaHielo.Cook () (at Assets/EsferaHielo.js:46)
UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(String)
EsferaHielo:Update() (at Assets/EsferaHielo.js:24)
Answer by polomeo · Aug 30, 2013 at 05:00 AM
Nevermind people!
I found the answer.
I rewrite the code from scratch, using an example that I found in the forums.
This is the code working. The only thing left is to set and tweek the variables.
var cannonball : Transform;
var speed : float = 500; // speed is power per second
var powermax : float = 5000;
var powermin : float = 100;
var autoThrow : boolean = true;
private var projectile;
function Update()
{
if (Input.GetButtonDown("Fire1"))
StartCoroutine("Cook");
}
// This is a co-routine.
// I used the term "to cook a grenade" as popular in many action games.
function Cook()
{
var power : float = powermin;
projectile = Instantiate(cannonball,
transform.position,
transform.rotation);
while (Input.GetButton("Fire1"))
{
var move = speed * Time.deltaTime;
power = Mathf.MoveTowards(power, powermax, move);
var size : float = 2.0;
var maxTamanio : Vector3 = Vector3(2.0, 2.0, 2.0);
projectile.localScale += Vector3(0.1,0.1,0.1);
if (projectile.localScale.magnitude >= maxTamanio.magnitude)
{projectile.localScale = Vector3(2.0, 2.0, 2.0);}
if (autoThrow && power == powermax)
break;
else
yield;
}
Shoot(power);
}
function Shoot(power : float)
{
projectile.rigidbody.AddForce(transform.forward * power);
//Physics.IgnoreCollision(projectile.collider, collider);
}
Thanks a lot for all your help!
Answer by aldonaletto · Aug 30, 2013 at 02:10 AM
A possible cause for your problems is the projectile variable being untyped: the compiler doesn't know its type, thus using Transform properties like localScale or rigidbody generates runtime errors. Declare it as Transform and probably the errors will disappear:
private var projectile: Transform;
NOTE: The assignment nivelHielo = Estadisticas.lvl_hielo; will happen only once when the object to which EsferaHielo is attached is created - is this ok?
I put Transform as you say, but now the error comes in line 49. I guess its because can't use rigidbody properties on a transform :/
I also try to rewrite the code, following the FPS tutorial scripts. $$anonymous$$y new code is this:
#pragma strict
var nivelHechizos : Estadisticas;
var bolaHielo : GameObject;
var aumento : float = 0.0001;
var velocidadIncial : float = 20.0;
var consumo$$anonymous$$ana : int = 5;
private var nivelHielo : int;
private var maxTamanio : Vector3;
private var tamanio : float;
function Start(){
nivelHechizos = FindObjectOfType(Estadisticas);
nivelHielo = nivelHechizos.lvl_hielo;//GameObject.Find('Jugador').GetComponent('Estadisticas').lvl_hielo;
tamanio = nivelHielo * 1.5;
//maxTamanio = Vector3(tamanio, tamanio, tamanio);
}
function Update () {
if (Input.GetButtonDown("Fire1"))
{
StartCoroutine("CargarHechizo");
}
}
function CargarHechizo(){
var bolaCargada : GameObject = Instantiate (bolaHielo, transform.position, transform.rotation);
while (Input.GetButton("Fire1"))
{
var size: float = aumento * nivelHielo;
bolaCargada.transform.localScale += Vector3(size, size, size);
yield WaitForSeconds (1);
if(size > tamanio)
{
size = 0;
}
}
LanzarHechizo(bolaCargada);
}
function LanzarHechizo(hechizo : GameObject){
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
hechizo.rigidbody.AddForce(transform.forward);
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(hechizo.collider, transform.root.collider);
}
With this code apparendly im getting no errors, but when the iceball instantiates, do not grow in size, while I hold the mouse button, and wont shoot either when I release de mouse button.
Your answer
