Search Unity

[SOLVED]New instance of script error

Discussion in 'Scripting' started by LordKawaii, Feb 9, 2016.

  1. LordKawaii

    LordKawaii

    Joined:
    Nov 24, 2014
    Posts:
    10
    Hey guys. I'm completely flummoxed here. I am just trying to create a new instance of a class inside a script and as soon as I try to access any of the public variables in the class I get a null reference error.

    Here is the script that I'm instantiating:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BotState
    5. {
    6.     public bool hasBeenFired;
    7.     public bool hasBeenPickedUp;
    8.     public bool isCharging;
    9.     public bool isAttacking;
    10.     public bool isSwarming;
    11.     public float timeTillChange;
    12.  
    13.     public BotState()
    14.     {
    15.         hasBeenFired = false;
    16.         hasBeenPickedUp = false;
    17.         isCharging = false;
    18.         isAttacking = false;
    19.         isSwarming = false;
    20.         timeTillChange = 0;
    21.     }
    22. }
    23.  
    And here is where I instantiate it
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class NanoBotController : MonoBehaviour {
    6.     public float chargeDistance = .06f;
    7.     public float fireSpeed = 10;
    8.     public float maxVelocity = 10;
    9.  
    10. //    [HideInInspector]
    11.     Rigidbody2D rb2d;
    12.     GameObject swarmTarget;
    13.     BotSwarmController swarmCon;
    14.     public BotState botState;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         botState = new BotState();
    19.         try
    20.         {
    21.             botState.timeTillChange = Time.time + Random.Range(0, swarmCon.frequencyOfChange);
    22.         }
    23.         catch
    24.         {
    25.             if (botState == null)
    26.                 Debug.LogError("botState is null");
    27.         }
    28.  
    29.         rb2d = gameObject.GetComponent<Rigidbody2D>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update () {
    34.         if (botState.isSwarming && botState.hasBeenPickedUp)
    35.         {
    36.             if (botState.timeTillChange >= Time.time)
    37.             {
    38.                 swarmCon.Swarm(rb2d);
    39.                 botState.timeTillChange = Time.time + Random.Range(0, swarmCon.frequencyOfChange);
    40.             }
    41.  
    42.             swarmCon.RotateTowards(swarmTarget.transform.position, transform);
    43.         }
    44.  
    45.         if (transform.position.y > 15 || transform.position.y < -15)
    46.         {
    47.             Destroy(gameObject);
    48.         }
    49.  
    50.     }
    51.  
    52.     void OnTriggerEnter2D(Collider2D other)
    53.     {
    54.         if (other.tag == "Enemy")
    55.             Attack(other.gameObject);
    56.     }
    57.  
    58.     void Attack(GameObject target)
    59.     {
    60.         if (!botState.isAttacking && !botState.hasBeenPickedUp)
    61.         {
    62.             rb2d.velocity = new Vector2(0, 0);
    63.             botState.isAttacking = true;
    64.             rb2d.isKinematic = true;
    65.             transform.parent = target.transform;
    66.            
    67.         }
    68.     }
    69.  
    70.     public void EndAttack()
    71.     {
    72.         if (botState.isAttacking)
    73.         {
    74.             rb2d.velocity = new Vector2(0, -1);
    75.             rb2d.isKinematic = false;
    76.             transform.parent = null;
    77.             botState.isAttacking = false;
    78.         }
    79.     }
    80.  
    81.     public void PickUp()
    82.     {
    83.         if (!botState.isAttacking)
    84.         {
    85.             swarmTarget = transform.parent.gameObject;
    86.             botState.hasBeenFired = false;
    87.             botState.hasBeenPickedUp = true;
    88.             botState.isCharging = false;
    89.             botState.isAttacking = false;
    90.             botState.isSwarming = true;
    91.             botState.timeTillChange = 0;
    92.         }
    93.         try
    94.         {
    95.             swarmCon = gameObject.GetComponentInParent<BotSwarmController>();
    96.             if (swarmCon == null)
    97.                 Debug.LogError("Nanobot was unable to find partents BotSwarmCon");
    98.         }
    99.         catch
    100.         {
    101.             Debug.LogError("Nanobot was unable to find partents BotSwarmCon");
    102.         }
    103.     }
    104.  
    105.     public void Charge(int numberCharged)
    106.     {
    107.         if (!botState.isCharging)
    108.         {
    109.             botState.isCharging = true;
    110.             rb2d.velocity = new Vector2(0,0);
    111.             transform.position = new Vector3(transform.parent.position.x, transform.parent.position.y + (chargeDistance * numberCharged));
    112.             transform.rotation = Quaternion.Euler(0, 0, 90);
    113.         }
    114.     }
    115.  
    116.     public void Fire()
    117.     {
    118.         botState.isSwarming = false;
    119.         botState.hasBeenPickedUp = false;
    120.         botState.isCharging = false;
    121.         botState.hasBeenFired = true;
    122.         transform.parent = null;
    123.         rb2d.AddForce(Vector2.up * fireSpeed);
    124.     }
    125.  
    126. }
    127.  
    128.  
    Right now it always errors out on line 61 of NanoBotController
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    line 61 in the code you've pasted is a curly bracket... o_O
     
  3. LordKawaii

    LordKawaii

    Joined:
    Nov 24, 2014
    Posts:
    10
    Oh, sorry. I did not double check the line numbers. Its line 60 that it starts to have a problem.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    can't see anything in there that would set the botState to null, and if it was null to start it would be complaining long before a function called from a triggerEnter... are there any other scripts that might set botstate to null?
     
  5. LordKawaii

    LordKawaii

    Joined:
    Nov 24, 2014
    Posts:
    10
    I can't think of any reason that for it to ever be set to null, but at least that's a direction for me to start looking.
     
    Last edited: Feb 10, 2016
  6. LordKawaii

    LordKawaii

    Joined:
    Nov 24, 2014
    Posts:
    10
    Well, I still don't know why it errored out there I was able to get it to stop after I found an object in the scene that should not have had the script. After I removed it from there the errors went away.

    Thinking about it though would a script that was not enabled cause something like this to happen... like maybe it did not fire off the Start function but still fired off something else?
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Just tried it and ye, don't ask me why but the trigger events are still fired even though it is disabled, however, start, update etc. are not being called so in that instance your variable wasn't set.

    Very interesting and good to know. I'm not sure whether this is intended or a bug though.
     
  8. LordKawaii

    LordKawaii

    Joined:
    Nov 24, 2014
    Posts:
    10
    Thanks for doing that test. I would have never thought that a disabled script could cause so much trouble.
     
    Suddoha likes this.