Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Yoshinator2 · Nov 11, 2016 at 12:37 AM · c#errorobject referenceobject-reference-error

Error Object reference not set to an instance of an object!!! Help, I am new!

I'm trying to make a 2D game where there are multiple brutes on the map at the same time. Here are my errors that I am getting: Upon starting the game: NullReferenceException: Object reference not set to an instance of an object bruteController.Start () (at Assets/Scripts/bruteController.cs:23)

My three scripts that are used for this are:

LevelManager: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class LevelManager : MonoBehaviour {

 //Adds a defineable delay to the attack
 public float playerAtAnLength;

 //Says whether or not the player can attack
 private bool playerCanAttack;

 //Use this to reference the playerController script
 public playerController thePlayer;

 //Use this to reference the bruteController script
 public bruteController theBrute;

 public bruteDeath theBruteDeath;

 //Used to store the brutes last position.
 public Vector3 bruteLastPosition;

 //Adds a defineable delay to the animation
 public float playerAtAnDelay;

 //Adds a defineable delay to the actual attack
 public float playerAcAtDelay;

 //Tracks the players velocity
 public float playerVelocity;

 //Allows you to define the amount that the attack should be moved left
 public double moveLeftDist;

 //Player Actual Attack Remove Time is the time that it takes for the attack collider to be removed
 public float playerAcAtReTi;

 //Allows me to set the object which would be spawned when the player is attacking
 public GameObject playerActualAttackCollider;

 //Is true if the last move was right
 public bool lastMoveRight;

 //Allows the conversion of the players position to a double
 private double playerTransformXDouble;

 //Allows you to set to players attack x location when looking left
 private Vector3 playerLocationLeft;

 //Allows you to define the players x position moved to the left
 private float playerAttackMovedLeft;

 // Use this for initialization
 void Start () {
     //Defines what thePlayer is in the playerController script
     thePlayer = FindObjectOfType<playerController>();
     theBruteDeath = FindObjectOfType<bruteDeath>();
     theBrute = FindObjectOfType<bruteController>();
     List<GameObject> Brute = new List<GameObject>();
     //Starts it out so that the player can attack
     playerCanAttack = true;

 }
 
 // Update is called once per frame
 void Update () {

     //
     //
     //All needed to flip the player attack when moving left
     //
     //
     //theBruteDeath = FindObjectOfType<bruteDeath>();
     //theBrute = FindObjectOfType<bruteController>();
     //Sets the playerVelocity to the live playerVelocity
     playerVelocity = thePlayer.playerVelocity;
     //Transfers the lastMoveRight variable to this script
     lastMoveRight = thePlayer.lastMoveRight;
     //Converts the position to a double
     playerTransformXDouble = thePlayer.transform.position.x;
     //Converts the double to a float
     playerAttackMovedLeft = (float)(playerTransformXDouble - moveLeftDist);
     //Sets the attack to the left of the player by a certain amount
     playerLocationLeft = new Vector3(playerAttackMovedLeft, thePlayer.transform.position.y, thePlayer.transform.position.z);
 }

 //
 //
 //
 //THIS CODE HAPPENS WHEN THE PLAYER ATTACKS OR ATTEMPTS TO ATTACK:
 //
 //
 //

 //Is able to be called from the playerController when the player attacks
 public void playerAttack()
 {
     //Will run if the player can attack
     if(playerCanAttack == true)
     {
         //Calls the playerAttackCo() to start
         StartCoroutine("playerAttackCo");
     }
 }
 //The Co-routine to add the delay into the attack, so you cant spam attack.
 public IEnumerator playerAttackCo()
 {
     //Makes the player not able to spam the attack button
     playerCanAttack = false;
     //Starts both co-routines below
     StartCoroutine("playerAtAnCo");
     //Will run if the player is moving right
     if(playerVelocity >= 0.1)
     {
         StartCoroutine("playerAcAtCoRight");
     }
     //Will run if the player is still
     if(playerVelocity == 0)
     {
         StartCoroutine("playerAcAtCoStill");
     }
     //Will run if the player is moving left
     if(playerVelocity <= -0.1)
     {
         StartCoroutine("playerAcAtCoLeft");
     }
     //Sets the canMove variable in the playerController to false
     thePlayer.GetComponent<playerController>().canMove = false;
     //Adds the actual delay for the number of seconds that playerAttackDelay is defined to.
     yield return new WaitForSeconds(playerAtAnLength);
     //Sets the canMove variable in the playerController to true
     thePlayer.GetComponent<playerController>().canMove = true;
     //When the delay is over, the player can attack again.
     playerCanAttack = true;
 }
 //This Co-routine adds enough delay so that the actual animation is played only once
 public IEnumerator playerAtAnCo()
 {
     //Sets the isAttacking variable in the playerController to true
     thePlayer.GetComponent<playerController>().isAttacking = 1;

     //Adds enough delay so that the animation is displayed
     yield return new WaitForSeconds(playerAtAnDelay);

     //Sets the isAttacking variable in the playerController to false
     thePlayer.GetComponent<playerController>().isAttacking = 0;
 }

 //This Co-routine adds the actual damage part into the attack
 public IEnumerator playerAcAtCoRight()
 {
     //Sets the delay for the actual damage to be given
     yield return new WaitForSeconds(playerAcAtDelay);
     //Spawns the attack radius
     Instantiate(playerActualAttackCollider, thePlayer.transform.position, thePlayer.transform.rotation);
 }
 //Lists all of the brutes?
 public List<bruteController> Brute
 {
     //Gets a brute
     get;
     //Sets the brute as a current brute.
     set;
 }
 public IEnumerator playerAcAtCoLeft()
 {
     //Sets the delay for the actual damage to be given
     yield return new WaitForSeconds(playerAcAtDelay);

     //Still need to flip instantiate transform position
     Instantiate(playerActualAttackCollider, playerLocationLeft, thePlayer.transform.rotation);
 }

 public IEnumerator playerAcAtCoStill()
 {
     //Sets the delay for the actual damage to be given
     yield return new WaitForSeconds(playerAcAtDelay);
     //Will run if the last move was right
     if(lastMoveRight == true)
     {
         //Will run and place the attack radius on the right if the player is facing right
         Instantiate(playerActualAttackCollider, thePlayer.transform.position, thePlayer.transform.rotation);
     }
     //Will run if the last move is left
     if (lastMoveRight == false)
     {
         //Will run and place the attack radius on the left if is the player is facing left
         Instantiate(playerActualAttackCollider, playerLocationLeft, thePlayer.transform.rotation);
     }
 }

}

bruteDeath: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class bruteDeath : MonoBehaviour {

 private bruteHealth theBrute;

 public Sprite deadBrute;

 private Animator myAnim;

 public float timeToEnd;

 public LevelManager theLevelManager;

 public Sprite bruteSprite;

 public Vector3 brutePosition;

 public Vector3 bruteRotation;

 public GameObject deadBody;

 private Vector3 bruteLastPosition;







 //The health of the brute
 public float bruteHealthPoints;

 //References the playerController script
 private playerController thePlayer;

 private float playerAt1Dam;







 // Use this for initialization
 void Start()
 {
     theLevelManager = FindObjectOfType<LevelManager>();
     theBrute = FindObjectOfType<bruteHealth>();
     myAnim = GetComponent<Animator>();
     bruteSprite = GetComponent<Sprite>();



     //Actually finds the object with the playerController script
     thePlayer = FindObjectOfType<playerController>();
     //Sets the player Attack 1 Damage in this script to the defined amount in the playerController
     playerAt1Dam = thePlayer.playerAttack1Dam;
 }

 // Update is called once per frame
 void Update() {
     //Checks to see if the brute has run out of health
     if (bruteHealthPoints <= 0)
     {
         //If the brute has no health, then they are removed from the world. Eventually, add an Instantiate with particales
         bruteAnimDeath();
     }
 }
 public void bruteActualDeath()
 {
     bruteLastPosition = new Vector3(theBrute.transform.position.x, theBrute.transform.position.y, theBrute.transform.position.z);
     addDeadBrute();
 }
 public void bruteAnimDeath()
 {
     myAnim.SetBool("bruteIsDead", true);
 }
 public void bruteDeathAnimDone()
 {
     myAnim.SetBool("bruteDeathAnimDone", true);
     bruteActualDeath();
 }
 public void addDeadBrute()
 {
     Destroy(gameObject);
     Instantiate(deadBody, bruteLastPosition, theBrute.transform.rotation);
 }
 //Kills the brute
 public void Kill()
 {

 }




 public void thePlayerAttack1BruteDamage()
 {
     //Takes the player attack 1 damage amount away from the brutes health.
     bruteHealthPoints = bruteHealthPoints - playerAt1Dam;
 }

}

bruteController: using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(bruteDeath))] public class bruteController : MonoBehaviour { //Creates the RigidBody for the brute private Rigidbody2D bruteRigidBody;

 public LevelManager theLevelManager;

 public bruteDeath bruteDeath;
 // Use this for initialization
 void Start () {
     //Gets the needed component for the Rigid Body
     bruteRigidBody = GetComponent<Rigidbody2D>();
     //Defines theLevelManager to find the object with the script attached
     theLevelManager = FindObjectOfType<LevelManager>();

     bruteDeath = GetComponent<bruteDeath>();
     //Registers you to the Level Manager
     theLevelManager.Brute.Add(this);
     //Makes the bruteDeath register as the script
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 //Occurs whenever the goblin enters a trigger collider
 void OnTriggerEnter2D(Collider2D other)
 {
     //Checks to see if the collider was the players attack radius
     if(other.tag == "playerAttack1")
     {
         //Starts the code in the LevelManager if the player attacked the brute
         bruteDeath.thePlayerAttack1BruteDamage();
     }
 }
 public void Kill()
 {
     theLevelManager.Brute.Remove(this);
     bruteDeath.Kill();
 }

}

Please note that line 23 of the bruteController is: theLevelManager.Brute.Add(this);

That code interacts with the LevelManagers List with the get; and set; I am not familiar with lists so an explanation on them would be awesome too! Thanks.

Comment
Add comment · Show 4
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 Filhanteraren · Nov 11, 2016 at 02:03 AM 0
Share

Does your Level$$anonymous$$anager exist in the scene?

avatar image Yoshinator2 Filhanteraren · Nov 11, 2016 at 02:05 AM 0
Share

Yes, it is a gameobject

avatar image Filhanteraren Yoshinator2 · Nov 11, 2016 at 02:11 AM 0
Share

You are not really using the Brute list anywhere it seems, so you might as well remove it

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

267 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

Why is object reference not set to an instance? 1 Answer

Object reference not set to an instance of an object 0 Answers

Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers

PLEASE HELP!! Object reference not set to an instance of an object at ChangeMasterVolume.Update 1 Answer

object reference not set to instance of an object On last line 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