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 jeff.teoh · Jun 19, 2016 at 09:58 AM · 2d game

Got an Unassigned error while trying to play my game.

Hi, I got this error while I was trying to play my game.

UnassignedReferenceException: The variable rightEdge of Enemy has not been assigned. You probably need to assign the rightEdge variable of the Enemy script in the inspector. UnityEngine.Transform.get_position () Enemy.Move () (at Assets/Scripts/Enemy.cs:184) PatrolState.Execute () (at Assets/Scripts/EnemyStates/PatrolState.cs:21) Enemy.Update () (at Assets/Scripts/Enemy.cs:115)

I can't figure out what happened.

Here's my Enemy.cs script:

using UnityEngine; using System.Collections; using System;

/// /// The enemy's class /// public class Enemy : Character {

 /// <summary>
 /// The enemy's current state
 /// changing this will change the enemys behaviour
 /// </summary>
 private IEnemyState currentState;

 /// <summary>
 /// The enemy's target
 /// </summary>
 public GameObject Target { get; set; }

 /// <summary>
 /// The enemy's melee range, at what range does the enemy need to use the sword
 /// </summary>
 [SerializeField]
 private float meleeRange;

 /// <summary>
 /// The enemy's throw range, how far can it start throwing knifes
 /// </summary>
 [SerializeField]
 private float throwRange;

 private Vector3 startPos;

 [SerializeField]
 private Transform leftEdge;

 [SerializeField]
 private Transform rightEdge;

 private Canvas healthCavas;

 /// <summary>
 /// Indicates if the enemy is in melee range
 /// </summary>
 public bool InMeleeRange
 {
     get
     {
         if (Target != null)
         {
             return Vector2.Distance(transform.position, Target.transform.position) <= meleeRange;

         }

         return false;
     }
 }

 /// <summary>
 /// Indicates if the enemy is in throw range
 /// </summary>
 public bool InThrowRange
 {
     get
     {
         if (Target != null)
         {
             return Vector2.Distance(transform.position, Target.transform.position) <= throwRange;

         }

         return false;
     }
 }

 /// <summary>
 /// Indicates if the enemy is dead
 /// </summary>
 public override bool IsDead
 {
     get
     {
         return healthStat.CurrentValue <= 0;
     }
 }

 // Use this for initialization
 public override void Start()
 {   //Calls the base start
     base.Start();

     this.startPos = transform.position;

     //Makes the RemoveTarget function listen to the player's Dead event
     Player.Instance.Dead += new DeadEventHandler(RemoveTarget);

     //Sets the enemy in idle state
     ChangeState(new IdleState());

     healthCavas = transform.GetComponentInChildren<Canvas>();
 }



 // Update is called once per frame
 void Update()
 {
     if (!IsDead) //If the enemy i alive
     {
         if (!TakingDamage) //if we aren't taking damage
         {
             //Execute the current state, this can make the enemy move or attack etc.
             currentState.Execute();
         }

         //Makes the enemy look at his target
         LookAtTarget();
     }

 }

 /// <summary>
 /// Removes the enemy's target
 /// </summary>
 public void RemoveTarget()
 {
     //Removes the target
     Target = null;

     //Changes the state to a patrol state
     ChangeState(new PatrolState());
 }

 /// <summary>
 /// Makes the enemy look at the target
 /// </summary>
 private void LookAtTarget()
 {
     //If we have a target
     if (Target != null)
     {
         //Calculate the direction
         float xDir = Target.transform.position.x - transform.position.x;

         //If we are turning the wrong way
         if (xDir < 0 && facingRight || xDir > 0 && !facingRight)
         {
             //Look in the right direction
             ChangeDirection();
         }
     }
 }

 /// <summary>
 /// Changes the enemy's state
 /// </summary>
 /// <param name="newState">The new state</param>
 public void ChangeState(IEnemyState newState)
 {
     //If we have a current state
     if (currentState != null)
     {
         //Call the exit function on the state
         currentState.Exit();
     }

     //Sets the current state as the new state
     currentState = newState;

     //Calls the enter function on the current state
     currentState.Enter(this);
 }

 /// <summary>
 /// Moves the enemy
 /// </summary>
 public void Move()
 {
     //If the enemy isn't attacking
     if (!Attack)
     {
         if ((GetDirection().x > 0 && transform.position.x < rightEdge.position.x) || (GetDirection().x < 0 && transform.position.x > leftEdge.position.x))
         {
             //Sets the speed to 1 to player the move animation
             MyAnimator.SetFloat("speed", 1);

             //Moves the enemy in the correct direction
             transform.Translate(GetDirection() * (movementSpeed * Time.deltaTime));
         }
         else if (currentState is PatrolState)
         {
             ChangeDirection();
         }

     }

 }

 /// <summary>
 /// Gets the current direction
 /// </summary>
 /// <returns>The direction</returns>
 public Vector2 GetDirection()
 {
     return facingRight ? Vector2.right : Vector2.left;
 }

 /// <summary>
 /// If the enemy collides with an object
 /// </summary>
 /// <param name="other">The colliding object</param>
 public override void OnTriggerEnter2D(Collider2D other)
 {
     //calls the base on trigger enter
     base.OnTriggerEnter2D(other);

     //Calls OnTriggerEnter on the current state
     currentState.OnTriggerEnter(other);
 }

 /// <summary>
 /// Makes the enemy take damage
 /// </summary>
 /// <returns></returns>
 public override IEnumerator TakeDamage()
 {
     if (!healthCavas.isActiveAndEnabled)
     {
         healthCavas.enabled = true;
     }

     //Reduces the health
     healthStat.CurrentValue -= 10;

     if (!IsDead) //If the enemy isn't dead then play the damage animation
     {
         MyAnimator.SetTrigger("damage");
     }
     else //If the enemy is dead then make sure that we play the dead animation
     {
         GameObject coin = (GameObject)Instantiate(GameManager.Instance.CoinPrefab, new Vector3(transform.position.x, transform.position.y + 5), Quaternion.identity);
         Physics2D.IgnoreCollision(GetComponent<Collider2D>(), coin.GetComponent<Collider2D>());
         MyAnimator.SetTrigger("die");
         yield return null;
     }
 }

 /// <summary>
 /// Removes the enemy from the game
 /// </summary>
 public override void Death()
 {
     MyAnimator.ResetTrigger("die");
     MyAnimator.SetTrigger("idle");
     healthStat.CurrentValue = healthStat.MaxVal;
     transform.position = startPos;
     healthCavas.enabled = false;
 }

 public override void ChangeDirection()
 {
     Transform tmp = transform.FindChild("EnemyHealthBarCanvas").transform;
     Vector3 pos = tmp.position;
     tmp.SetParent(null);

     base.ChangeDirection();

     tmp.SetParent(transform);
     tmp.position = pos;
 }

}

Patrol state script:

using UnityEngine; using System.Collections;

public class PatrolState : IEnemyState { private Enemy enemy; private float patrolTimer; private float patrolDuration;

 public void Enter(Enemy enemy)
 {
     this.enemy = enemy;
     patrolDuration = Random.Range(1, 10);
 }

 public void Execute()
 {
     Patrol();

     enemy.Move();

     if (enemy.Target != null && enemy.InThrowRange)
     {
         enemy.ChangeState(new RangedState());
     }
 }

 public void Exit()
 {
    
 }

 public void OnTriggerEnter(Collider2D other)
 {
     if (other.tag == "Edge")
     {
         enemy.ChangeDirection();
     }
     if (other.tag == "Knife")
     {
         enemy.Target = Player.Instance.gameObject;
     }
 }

 private void Patrol()
 {
     patrolTimer += Time.deltaTime;

     if (patrolTimer >= patrolDuration)
     {
         enemy.ChangeState(new IdleState());
     }
 }

}

Comment
Add comment · Show 2
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 UsmanAbbasi · Jun 19, 2016 at 10:45 AM 0
Share

Post the script in which you are getting error. Post these scripts: "Enemy.cs" and "PatrolState.cs"

avatar image jeff.teoh · Jun 20, 2016 at 02:38 AM 0
Share

Any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jeff.teoh · Jun 20, 2016 at 03:56 AM

I fixed the error by just creating 2 game object and assign them to right edge and left edge.

Comment
Add comment · Share
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

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

61 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

Related Questions

How to create a build on a mac for a PC? 1 Answer

Stop the camera at the edge of the scene? 2DGame 0 Answers

IsTouchingLayers dont work : 0 Answers

Animation Panel: how to turn an existing loop into just a standing sprite and then implement it 0 Answers

How do you selectively mask objects based on what side they are on? 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