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 zolutr · Sep 23, 2016 at 12:58 AM · unity 5nullreferenceexceptionif-statementsrpgstate-machine

rpg enemy target gives null reference

after giving my enemy a target it shows a null reference in the editor, it seems to be in line 90

 if (HeroToAttack.GetComponent<HeroSM>() != null)
          {
              DoDamage();
          }

line that i added only to avoid any null reference that will show in line 105

 HeroToAttack.GetComponent<HeroSM>().TakeDamage(calcDamage);

the idea is that my enemy gets a random target from the heroes performing list, i do this using state machines.

here is the one of the enemy:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class EnemySM : MonoBehaviour {

  public EnemyStats Enemy;//creamos a enemigo desde la plantilla
  private BattleState BS;
  public bool actionStarted = false;
  public GameObject Description;
  private HandleTurn myAttack;
 
  public enum enemyTurn
  {
      PROCESSING,
      CHOOSEACTION,
      WAITING,
      ACTION,
      DEAD
  }
  public static enemyTurn currentEnemyTurn;
  private float curCooldown = 0f;
  private float maxCooldown = 8f;
 
  //para cambiar las barras
  private float curHealth;
  public GameObject HeroToAttack;
  // Use this for initialization
  void Start()
  {
      myAttack = new HandleTurn();
      currentEnemyTurn = enemyTurn.PROCESSING;
      BS = GameObject.Find("BM").GetComponent<BattleState>();
 
  }
  // Update is called once per frame
  void Update()
  {
 
         
      if (GMScript.InBattle == true)
      {            
          curHealth = Enemy.CurrentHp;
          switch (currentEnemyTurn)
          {
              case (enemyTurn.PROCESSING):
                  UpgradeBar();
                  break;
              case (enemyTurn.CHOOSEACTION):
                  //print("enemy chooseaction");
                  ChooseAction();
                  currentEnemyTurn = enemyTurn.WAITING;
 
                  break;
              case (enemyTurn.WAITING):
                  //idle state                   
                  break;
              case (enemyTurn.ACTION):
                 // print("enemy action");
                  StartCoroutine(TimeForAction());
 
                  break;
              case (enemyTurn.DEAD):
                  break;
          }
      }
  }
 
 
 
  void ChooseAction()
  {
      myAttack = new HandleTurn();
      myAttack.Attacker = Enemy.baseName;
      myAttack.Side = "Enemy";
      myAttack.AttackerGO = this.gameObject;
      myAttack.TargetGO = BS.HeroInGame[Random.Range(0, BS.HeroInGame.Count)];
      HeroToAttack = myAttack.TargetGO;
      int num = Random.Range(0, Enemy.Attacks.Count);//se genra un numero aleatorio entre 0 y la cantidad de ataques
      myAttack.chooseAttack = Enemy.Attacks[num];//usamos el numero para escoger el ataque
      Debug.Log(this.gameObject.name + " has choosen " + myAttack.chooseAttack.attackName + " and did " + myAttack.chooseAttack.attackDamage + " damage!!!");
      BS.CollectActions(myAttack);
  }
  private IEnumerator TimeForAction()//controla variables atraves del tiempo
  {
      
      if (actionStarted == true)
      {
          yield break;
      }
      actionStarted = true;
      //wait
      //do damage
      
      if (HeroToAttack.GetComponent<HeroSM>() != null)
      {
          DoDamage();
      }
          //remove from bs list
      BS.PerformList.RemoveAt(0);//remueve al objeto en 0, el siguiente sube transformando el 1 en 0
                                 //continua removiendo hasta desaparecer todo
                                 //reset bs to wait
      BS.battleStates = BattleState.performAction.WAIT;
      //al pasar a wait y saltar a takeaction los objetos vuelven a crearse repitiendo el ciclo
      //end coroutine
      actionStarted = false;
      currentEnemyTurn = enemyTurn.PROCESSING;
  }
  void DoDamage()
  {
      float calcDamage = Enemy.Fue * BS.PerformList[0].chooseAttack.attackDamage;
      print(calcDamage);
      HeroToAttack.GetComponent<HeroSM>().TakeDamage(calcDamage);
  }
 
  void UpgradeBar()
  {
      print(curCooldown);
      curCooldown = curCooldown + Time.deltaTime;
      if(curCooldown >= maxCooldown)
      {
          currentEnemyTurn = enemyTurn.CHOOSEACTION;
          curCooldown = 0f;
      }
  }
 

}

kind of a long script, i think the place to check is in the do damage or timeforaction functions but cant seem to find anything.

Comment
Add comment · Show 1
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 zolutr · Sep 23, 2016 at 12:59 AM 0
Share

it works with the help of another state machine wich also handles another herostatemachine but its just so much code for a question plus i doubt the error is there.

but just in case here are two small chunks that may be related to the problem

this is takedamage:

 public void TakeDamage(float getDamageAmount)
     {
         hero.CurrentHp -= getDamageAmount;
         if (hero.CurrentHp <= 0)
         {
             currentHeroTurn = heroTurn.DEAD;
         }
     }

and this interacts with the lists

 if (PerformList[0].Side == "Enemy")
     {
           EnemyS$$anonymous$$ ES$$anonymous$$ = performer.GetComponent<EnemyS$$anonymous$$>();
           ES$$anonymous$$.HeroToAttack = PerformList[0].TargetGO;
           EnemyS$$anonymous$$.currentEnemyTurn = EnemyS$$anonymous$$.enemyTurn.ACTION;
      }

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

110 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

Related Questions

Ray null reference exception and wrong direction when casting it 0 Answers

make character move and interact by itself final fantasy style 1 Answer

State Machine Behaviour OnStateExit doesn't call some time. 0 Answers

Why Am I Getting Null Reference Exception? 1 Answer

Detecting if Player is In Range of Items? 1 Answer


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