Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by yakirh95 · Jul 18, 2020 at 11:43 AM · referencing

Enemy movement after death

Hello guys I'm new to unity and coding in general , and trying to make my first game

So I managed to make the enemy show "death animation" after jumping on its head, but it still moves untill it's destroyed

Tried getting to the scrips of the enemy from the player's, but I get a console error- object reference not set to an instance of an object (line with EnemyMove.speed)

that's the player's script (cut it short for ya, tell me if I need to add the full script)

public class PlayerMovement : MonoBehaviour {

 //kill enemies
 public GameObject jumpkill;
 private Animator DieAnm;
 private EnemyMove EnemyMove;

 // Start is called before the first frame update
 void Start()
 {
   
     //enemy
     EnemyMove =  GetComponent<EnemyMove>();   
     DieAnm = jumpkill.GetComponent<Animator>();

 }

 private void OnCollisionEnter2D(Collision2D collision)
 {    
     if (collision.gameObject.tag == "Stomp")
     {
         Destroy(jumpkill, 1);
         DieAnm.SetTrigger("Die");
         EnemyMove.speed = 0;

     }
 }

}

that's the enemy script

 public class EnemyMove : MonoBehaviour {
   
 public float speed;
 public Vector2 movement;

 
 public Animator DieAnm;
 // Start is called before the first frame update
 void Start()
 {
     speed = 1 * Time.deltaTime;
     movement = new Vector2(speed, 0);
   
 }

 // Update is called once per frame
 void Update()
 {
     transform.position = new Vector2(transform.position.x + movement.x, transform.position.y);
     if (transform.position.x >= 41)
     {
         movement = new Vector2(-speed, 0);
         transform.localScale = new Vector2(-0.45f, 0.45f);
     }

     if (transform.position.x <= 34)
     {
         movement = new Vector2(speed, 0);
         transform.localScale = new Vector2(0.45f, 0.45f);
     }
 }  

}

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 gblim · Jul 18, 2020 at 12:46 PM 0
Share

Well first of all, what is jumpkill? and can you share a screen shot or better yet a GIF of whats going on?

avatar image segy0296 · Jul 18, 2020 at 02:01 PM 0
Share

$$anonymous$$aybe the OnCollisionEnter2D isnt being called. You can see if this is the case by placing Debug.Log() inside the condition. if (collision.gameObject.tag == "Stomp"){Debug.Log("PLAYER STO$$anonymous$$P");}

Then when colliding with the player you check if the Debug.Log() is being called. If not then your collision isnt being detected. $$anonymous$$aybe you havent set the "Stomp" tag to the object.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by frederikedel · Jul 18, 2020 at 12:55 PM

@yakirh95 I don't quite understand where the enemymove Script is attached to and what jumpkill is, but I can probably explain the Error Message:

          Destroy(jumpkill, 1);
          DieAnm.SetTrigger("Die");
          EnemyMove.speed = 0;

If I understood correctly, EnemyMove is attached to jumpkill?!. If thats the case, you're destroying the GameObject and then trying to access the script, which was attached to jumpkill. But its gone and the reference you did with EnemyMove = GetComponent<EnemyMove>(); doesnt exist anymore. Thats why it say Reference not set to an Instance.. . Just try swapping the speed = 0 and the Destroy() Line.

Comment
Add comment · Show 3 · 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
avatar image yakirh95 · Jul 18, 2020 at 01:56 PM 0
Share

I'm sorry if I wasnt clear. Enemy$$anonymous$$ove is the enemy's script. I created a collider on the enemy's head (tagged it "stomp"), and then created GameObject in the player's script (called it jumpkill), and dragged the enemy into it. whenever I hit the collider, the enemy is destoryed.

changing the lines order did'nt work =(

avatar image frederikedel yakirh95 · Jul 19, 2020 at 12:38 PM 1
Share

But in this case, this line Enemy$$anonymous$$ove = GetComponent<Enemy$$anonymous$$ove>(); makes no sense. You're searching for the Enemy$$anonymous$$ove Script in the PlayerController, which is probably on the player. But the Script is on the Enemy.So it returns null exception, because it cannot be found. Here's what you could try: Enemy$$anonymous$$ove = jumkill.GetComponent<Enemy$$anonymous$$ove>(); If you're unsure what you have to reference you can write:

 [SerializeField] private Enemy$$anonymous$$ove Enemy$$anonymous$$ove;

SerializeField allows you to reference something through the inspector while still having the gamobject be private.

avatar image yakirh95 frederikedel · Jul 19, 2020 at 05:26 PM 1
Share

Thanks alot

adding jumpkill worked

avatar image
0

Answer by gblim · Jul 18, 2020 at 03:23 PM

Try changing EnemyMove = GetComponent<EnemyMove>(); to EnemyMove = GameObject.FindObjectOfType<EnemyMove>();

Comment
Add comment · Show 1 · 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
avatar image yakirh95 · Jul 18, 2020 at 03:41 PM 0
Share

did'nt work either

avatar image
0

Answer by Bezoro · Jul 19, 2020 at 08:13 PM

You are trying to access the enemy speed after marking it for destruction, Unity won't allow that so it throws an exception.

Btw, handling the logic for killing enemies directly in the player is a pretty bad idea. The enemy itself should handle all that.

So instead of the player getting references to the enemy and handling all the logic for killing the enemy it should simply call a function inside the enemy class that handles everything.

The way you have it setup right now is very very bad practice and will bite you later.

There is no reason for the player to know how or why the enemy works the way it works, it should just check for the collision then tell the enemy to die.

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

133 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

Related Questions

How do I hold an object within one of my spheres, to where I can click the sphere and activate the object? 0 Answers

Referencing instantiated player in camera's Start() 2 Answers

Set AsyncOperation.allowSceneActivation from another script ? 1 Answer

reference to different classes : item held by different type of owner 2 Answers

I am having trouble making a reference. 2 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