Search Unity

Unity having problems accessing Bools from another Script...

Discussion in 'Scripting' started by ToastHatter, Aug 14, 2017.

  1. ToastHatter

    ToastHatter

    Joined:
    Nov 11, 2016
    Posts:
    43
    I have created a script that randomly applies a buff to both players in my game, it first accesses the players scripts and sets a certain bool to on or off to avoid the effects stacking within the script, however i keep getting this error for the bools from the other scripts.

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Slots+<Spin>c__Iterator0.MoveNext () (at Assets/Slots.cs:85)
    3. UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    4. UnityEngine.MonoBehaviour:StartCoroutine(String)
    5. Slots:Start() (at Assets/Slots.cs:19)
    6.  
    Btw, the line for where the error occurs depends on the number generated.

    here is the code which is having problems.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Slots : MonoBehaviour {
    6.  
    7.     public Animator anim;
    8.     public Player Player1;
    9.     public Player Player2;
    10.  
    11.     public bool Giga;
    12.     public bool Speed;
    13.     public bool Fire;
    14.     public bool Dark;
    15.  
    16.     public int random;
    17.     // Use this for initialization
    18.     void Start () {
    19.         StartCoroutine ("Spin");
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         Player1 = GameObject.FindWithTag ("Player").GetComponent<Player> ();
    25.         Player2 = GameObject.FindWithTag ("Player2").GetComponent<Player> ();
    26.  
    27.         anim.SetBool ("Giga", Giga);
    28.         anim.SetBool ("Speed", Speed);
    29.         anim.SetBool ("Fire", Fire);
    30.         anim.SetBool ("Dark", Dark);
    31.  
    32.     }
    33.  
    34.     IEnumerator Spin(){
    35.         random = Random.Range (1, 5);
    36.  
    37.         if (random == 1) {
    38.             Giga = true;
    39.  
    40.             Player1.Big = true;
    41.             Player1.transform.localScale = new Vector3 (2, 2, 2);
    42.             Player2.Big = true;
    43.             Player2.transform.localScale = new Vector3 (2, 2, 2);
    44.  
    45.             yield return new WaitForSeconds (20f);
    46.  
    47.             Player1.Big = false;
    48.             Player1.transform.localScale = new Vector3 (1, 1, 1);
    49.             Player2.Big = false;
    50.             Player2.transform.localScale = new Vector3 (1, 1, 1);
    51.         }
    52.  
    53.         if (random == 2) {
    54.             Speed = true;
    55.  
    56.             Player1.Fast = true;
    57.             Player1.maxSpeed = Player1.maxSpeed * 2f;
    58.             Player2.Fast = true;
    59.             Player2.maxSpeed = Player2.maxSpeed * 2f;
    60.  
    61.             yield return new WaitForSeconds (20f);
    62.  
    63.             Player1.Fast = false;
    64.             Player1.maxSpeed = Player1.maxSpeed / 2f;
    65.             Player2.Fast = false;
    66.             Player2.maxSpeed = Player2.maxSpeed / 2f;
    67.         }
    68.  
    69.         if (random == 3) {
    70.             Fire = true;
    71.  
    72.             Player1.Fire = true;
    73.             Player2.Fire = true;
    74.  
    75.             yield return new WaitForSeconds (20f);
    76.  
    77.             Player1.Fire = false;
    78.             Player2.Fire = false;
    79.  
    80.         }
    81.  
    82.         if (random == 4) {
    83.             Dark = true;
    84.  
    85.             Player1.Grav = true;
    86.             Player1.rb2d.gravityScale = Player1.rb2d.gravityScale / 3f;
    87.             Player1.rb2d.mass = Player1.rb2d.mass / 1.2f;
    88.             Player2.Grav = true;
    89.             Player2.rb2d.gravityScale = Player2.rb2d.gravityScale / 3f;
    90.             Player2.rb2d.mass = Player2.rb2d.mass / 1.2f;
    91.  
    92.             yield return new WaitForSeconds (20f);
    93.  
    94.             Player1.Grav = false;
    95.             Player1.rb2d.gravityScale = Player1.rb2d.gravityScale * 3f;
    96.             Player1.rb2d.mass = Player1.rb2d.mass * 1.2f;
    97.             Player2.Grav = false;
    98.             Player2.rb2d.gravityScale = Player2.rb2d.gravityScale * 3f;
    99.             Player2.rb2d.mass = Player2.rb2d.mass * 1.2f;
    100.  
    101.         }
    102.  
    103.  
    104.     }
    105. }
    106.  
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    My guess is that Player1 and or Player2 are null references. The error should pinpoint the exact line where the object you're referring to is null. But my money is on one of those. Bear in mind a couple things with GameObject.FindWithTag

    1) it's case sensitive. So your tag really needs to be that same case
    2) it's probably a bad idea to be doing this every update frame. Is it not possible to set these through the inspector? Is it possible that they'll be there one frame, but not another? If not, then I'd tackle that a different way. And well, if it's possible they don't exist at some point, then that definitely explains the error you get. You're trying to access an object that doesn't exist.

    Anyways, check your error and see if it's on a line where you're referencing Player1 or Player2 object.

    You can do something like check if the object is null before accessing it... or if you'll always have a player 1 and player 2 and they don't change, set them in the inspector.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    Code (CSharp):
    1. Player1 = GameObject.FindWithTag ("Player").GetComponent<Player> ();
    2.         Player2 = GameObject.FindWithTag ("Player2").GetComponent<Player> ();
    Don't do this in update. Do it in start. You shouldn't need to get the component over and over again each frame.

    But the reason you get the error is also because of this. Start always happens the first frame, which means it triggers before update does. Which means Player1 and Player2 have no value when the IEnumerator runs. So you need to do those two lines in start before you start your coroutine.

    Edit: @cstooch did mention the other thing of just setting in the inspector if they don't change which would also fix the issue, but again, no reason to do this in update.
     
    ToastHatter likes this.
  4. ToastHatter

    ToastHatter

    Joined:
    Nov 11, 2016
    Posts:
    43

    Oh thank you, I moved the chunk of code to the start and it now applies the buffs, thank you! :)