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 IsaacHoward · Dec 11, 2015 at 01:07 AM · c#unity 5

object reference not set to the instance of an object line 29. i want to get script of playermovement and if isattacking equal true i want to kill enemy. c# unity5

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour { public float playerDistance; public Rigidbody2D playerRB2D;

public PlayerMovement playerMovement { get { return GetComponent(); } }

 // Use this for initialization
 void Start() {

 }

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

     GetComponent<PlayerMovement>();


     playerDistance = Vector3.Distance(playerRB2D.transform.position, transform.position);

     if (playerMovement.isAttacking == true) //this line 29
     {

         if (playerMovement.isAttacking == true && playerDistance <= 1)
         {
             Debug.Log("Die");
             Destroy(gameObject);
             playerMovement.isAttacking = false;
         }

     }
 }

}


using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

 private Rigidbody2D rbody;
 private Animator anim;
 public bool isAttacking = false;
 //public float enemyDistance;
 //public Rigidbody2D enemyRB2D;

  

 // Use this for initialization
 void Start () {
     rbody = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void Update () {
     Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal") , Input.GetAxisRaw("Vertical"));


     //if not zero must be moving
     if (movement_vector != Vector2.zero)
     {
         anim.SetBool("isWalking", true);
         anim.SetFloat("input_x", movement_vector.x);
         anim.SetFloat("input_y", movement_vector.y);
     }
     else //sees if in idel or not changes anim
     {
         anim.SetBool("isWalking", false);

     }
     //movment by 1
     rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);

     //sets attack to true if space hit
     if (Input.GetKeyDown(KeyCode.Space))
     {
         isAttacking = true;
         Debug.Log("Attack");
   
     }
     //Makes anim attack
     if (isAttacking == true)
     {
         anim.SetBool("isAttacking", true);

         isAttacking = false;

     }
     else
     {
         anim.SetBool("isAttacking", false);
     }

    




 }

 // checks if touch enemy and then if can attack then kill
 //void OnTriggerEnter2D(Collider2D other)
 //{
 //    {
 //        if (other.gameObject.tag == ("Enemy"))
 //        {
 //            enemyDistance = Vector3.Distance(enemyRB2D.transform.position, transform.position);
 //            Debug.Log("Touch");
 //            if (isAttacking == true && enemyDistance <= 1)
 //            {
 //                Debug.Log("Die");
 //                Destroy(other.gameObject);
 //                isAttacking = false;
 //            }

 //        }
 //    }
 //}




}//end

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by beau101023 · Dec 11, 2015 at 02:04 AM

This may be your problem,

this:

 GetComponent<PlayerMovement>();

Should be this:

 playerMovement = GetComponent<PlayerMovement>();
Comment
Add comment · Show 2 · 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 IsaacHoward · Dec 11, 2015 at 03:01 AM 0
Share

Now it i have another error called CS0200 Property or indexer 'Enemy.player$$anonymous$$ovement' cannot be assigned to -- it is read only

avatar image IsaacHoward · Dec 11, 2015 at 03:52 AM 1
Share

I figured it out just add into update GameObject player = GameObject.Find("Player"); Player$$anonymous$$ovement player$$anonymous$$ovement = player.GetComponent ();

Then delete GetComponent();

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

45 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

Related Questions

How to use the results of a dice roll? 2 Answers

How do i Instantiate sub-Points with in Multiple Points?? 0 Answers

OnGUI will not show up? 1 Answer

My audio doesn't play, I'm through my options... 1 Answer

VS Code or visual studio? 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