Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by polomeo · Aug 29, 2013 at 07:24 PM · communicationleveling

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · Aug 29, 2013 at 07:26 PM 0
Share

What is the full text of the exception?

avatar image Owen-Reynolds · Aug 29, 2013 at 08:00 PM 0
Share

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.

avatar image polomeo · Aug 30, 2013 at 01:43 AM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image polomeo · Aug 30, 2013 at 04:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Webplayer: Switch to fullscreen via HTML-Button? 1 Answer

Needing to get communication between two c# scripts. 1 Answer

Switches that represent object state 0 Answers

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

Help with Unity communicating with Arduino (serial port communication) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges