Search Unity

Unity Crashes when using this script (NullReference?!)

Discussion in 'Editor & General Support' started by Pouya_Jigsaw, Mar 26, 2019.

  1. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    i wanted to make an arkanoid 2d game that instantiate balls every 5 seconds and when it instantiates, waits 1 second and then falls to the ground. here are the ball and ballSpawner Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Ball : MonoBehaviour
    7. {
    8.  
    9.     Rigidbody2D rb2d;
    10.     float startAngle = 20f;
    11.  
    12.     Timer ballDeathTimer;
    13.     BallSpawner ballSpawner;
    14.     Timer addForceTimer;
    15.     bool deadInScreen = false;
    16.     Vector2 colliderLowerLeft;
    17.     Vector2 colliderUpperRight;
    18.    
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.        
    23.  
    24.        
    25.        
    26.         ballSpawner = Camera.main.GetComponent<BallSpawner>();
    27.  
    28.        ballDeathTimer = gameObject.AddComponent<Timer>();
    29.         ballDeathTimer.Duration = ConfigurationUtils.BallLifeTime;
    30.         ballDeathTimer.Run();
    31.  
    32.         addForceTimer = gameObject.AddComponent<Timer>();
    33.         addForceTimer.Duration = 1f;
    34.         addForceTimer.Run();
    35.      
    36.      
    37.     }
    38.  
    39.    
    40.  
    41.     public void SetDirection(Vector2 direction)
    42.     {
    43.  
    44.         rb2d.velocity = - direction * rb2d.velocity.magnitude;
    45.     }
    46.     // Update is called once per frame
    47.     void Update()
    48.     {
    49.      
    50.             if (addForceTimer.Finished)
    51.             {
    52.                 AddForceToBall();
    53.                 RestoreForceTimer();
    54.             }
    55.        
    56.  
    57.        
    58.         if(ballDeathTimer.Finished)
    59.         {
    60.             Destroy(gameObject);
    61.             deadInScreen = true;
    62.             ballSpawner.SpawnABall();
    63.             addForceTimer.Run();
    64.          
    65.         }
    66.  
    67.        
    68.     }
    69.  
    70.     void AddForceToBall()
    71.     {
    72.         float startAngleRadian = 270 * Mathf.Deg2Rad;
    73.         Vector2 vectorForce = new Vector2(Mathf.Cos(startAngleRadian), Mathf.Sin(startAngleRadian));
    74.  
    75.         rb2d = GetComponent<Rigidbody2D>();
    76.         rb2d.AddForce(vectorForce * ConfigurationUtils.BallImpulseForce);
    77.     }
    78.  
    79.     void RestoreForceTimer()
    80.     {
    81.         Destroy(addForceTimer);
    82.         addForceTimer = gameObject.AddComponent<Timer>();
    83.         addForceTimer.Duration = 1f;
    84.     }
    85.     private void OnBecameInvisible()
    86.     {
    87.         if (!deadInScreen)
    88.         {
    89.             Destroy(gameObject);
    90.            // ballSpawner.SpawnABall();
    91.             RestoreForceTimer();
    92.  
    93.         }
    94.     }
    95.  
    96.  
    97.    
    98. }
    99.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallSpawner : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     GameObject ball;
    10.  
    11.  
    12.  
    13.     Timer spawnSeconds;
    14.    
    15.     float spawnX, spawnY;
    16.     BoxCollider2D boxCol2D;
    17.     Vector2 colliderLowerLeft;
    18.     Vector2 colliderUpperRight;
    19.    
    20.  
    21.  
    22.     private void Start()
    23.     {
    24.        
    25.         GameObject ballTemp = Instantiate(ball, Vector3.zero, Quaternion.identity);
    26.         boxCol2D = ballTemp.GetComponent<BoxCollider2D>();
    27.         colliderLowerLeft = (Vector2)transform.position - new Vector2(boxCol2D.size.x/2, boxCol2D.size.y/2);
    28.         colliderUpperRight = (Vector2)transform.position + new Vector2(boxCol2D.size.x/2, boxCol2D.size.y/2);
    29.         Destroy(ballTemp);
    30.  
    31.      
    32.  
    33.         spawnSeconds = gameObject.AddComponent<Timer>();
    34.         spawnSeconds.Duration = Random.Range(ConfigurationUtils.MinSpawnSecond,ConfigurationUtils.MaxSpawnSecond);
    35.         spawnSeconds.Run();
    36.        
    37.        
    38.     }
    39.    
    40.  
    41.     private void Update()
    42.     {
    43.        if(spawnSeconds.Finished)
    44.         {
    45.             SpawnABall();
    46.            
    47.            
    48.         }
    49.  
    50.      
    51.     }
    52.  
    53.  
    54.     public void SpawnABall()
    55.     {
    56.  
    57.            
    58.         GameObject ballSpawn = Instantiate(ball,RandomPosition(), Quaternion.identity);
    59.         while(Physics2D.OverlapArea(colliderLowerLeft, colliderUpperRight) != null)
    60.         {
    61.             Destroy(ballSpawn);
    62.             ballSpawn = Instantiate(ball, RandomPosition(), Quaternion.identity);
    63.             Debug.Log("trying again!");
    64.         }
    65.  
    66.              
    67.     }
    68.  
    69.     Vector2 RandomPosition()
    70.     {
    71.         spawnX = Random.Range(ScreenUtils.ScreenLeft, ScreenUtils.ScreenRight);
    72.         spawnY = Random.Range(ScreenUtils.ScreenBottom, ScreenUtils.ScreenTop);
    73.  
    74.         Vector2 positionSpawn = new Vector2(spawnX, spawnY);
    75.  
    76.         return positionSpawn;
    77.     }
    78.  
    79.  
    80.    
    81. }

    please use my code for a circle and try if you want. i get null Reference for timers, and the timer script is something standard i used it a lot. when it runs unity hangs, when opening task manager memory consumptions rise up every second and says its not responding.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the exact error and line number? You can use Debug.Log to determine if an object is null during a debug session.
     
  3. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    Code (CSharp):
    1.    if (addForceTimer.Finished)
    2.             {
    3.                 AddForceToBall();
    4.                 RestoreForceTimer();
    5.             }
    the if line, i get nullreferenece in every update execution. but now it just crashes.and i cant debug anymore.
     
  4. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    Ok i just used a ball object and debug.log and i figured out that the problem is not timer; but in an instant a whole lot of ball just instantiates one after another like crazy and it crashes unity

    upload_2019-3-27_14-2-6.png
     
  5. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    ok i found out the proble, thanks a lot jeff that sentence you told me about debug.log was just something i knew but it gave me insight into my problem.
    when timer finishes it should run again but i forgot to write that line. a simple line wasted my goddamn 3 days.
     
  6. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    well that piece of code is for finding collision-free space in screen. if you spawn a ball in a place there is another ball it just spawns another ball in a random place. it does not cause problem i tested it in a different scene and it worked fine.

    do you have any advice for that?

    and no im not saying its unity's fault maybe i posted in a wrong place, i certainly knew it was my problem. im new here.if im in a same problem, where should i post it?