Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't access player position from another script

Discussion in 'Scripting' started by Llamabro4, Jun 18, 2019.

  1. Llamabro4

    Llamabro4

    Joined:
    Jun 13, 2018
    Posts:
    13
    So right now im trying to make an enemy on a 2D plane check to see if the player.transform.position.x is either > or < the transform.position.x of the object. If so, it will either add a force negative or positive on the x value of the enemy to move the enemy towards the player. Except when i try this, the editor says that there is a null reference to the player and that i cant do player.transform.position.x. If anyone knows what im doing wrong, please tell me. All answers are appreciated, thanks

    Ignore "_isHopping"

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.  
    9.     private Player player;
    10.  
    11.     [SerializeField]
    12.     private bool _isHopping = false;
    13.     [SerializeField]
    14.     private float _jumpForce = 430.0f;
    15.     [SerializeField]
    16.     private float _sideForce = 200.0f;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         rb = gameObject.GetComponent<Rigidbody2D>();
    22.         player = gameObject.GetComponent<Player>();
    23.  
    24.         StartCoroutine(EnemyAction());
    25.     }
    26.  
    27.     //spin when jump
    28.     //on collission with player, die and drop coins
    29.  
    30.     IEnumerator EnemyAction()
    31.     {
    32.         while (true)
    33.         {
    34.             _isHopping = false;
    35.             yield return new WaitForSeconds(3.0f);
    36.             _isHopping = true;
    37.  
    38.             rb.AddForce(Vector3.up * _jumpForce);
    39.  
    40.             float playerPosX = player.transform.position.x;
    41.  
    42.             if (playerPosX > transform.position.x)
    43.             {
    44.                 rb.AddForce(Vector3.right * _sideForce);
    45.             }
    46.             else if (playerPosX < transform.position.x)
    47.             {
    48.                 rb.AddForce(Vector3.right * -_sideForce);
    49.             }
    50.  
    51.             yield return new WaitForSeconds(0.8f);
    52.         }
    53.     }
    54. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    gameObject.GetComponent<Player>() looks at the same gameobject that your Enemy script is on. I doubt you have both the enemy and the player script on the same gameobject in the scene.

    If you only have one Player script in the scene, you can just FindObjectOfType<Player> instead. But what might be better is to have a manager script in the scene that references your player. Then your enemy scripts access the player through that Manager script which could be a singleton
     
  3. Llamabro4

    Llamabro4

    Joined:
    Jun 13, 2018
    Posts:
    13
    Alright it works great! Also wait so, is FindObjectOfType<Player> referencing the Player gameobject or is it getting the component through the player?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    FindObjectOfType ransacks the entire scene looking for an instance of the Player Monobehavior... the first one it finds it returns. If there are none it returns null. This is not a particularly performant function and is ideally only called once at the start of a scene, traditionally in Start().
     
  5. Llamabro4

    Llamabro4

    Joined:
    Jun 13, 2018
    Posts:
    13
    Ahh ok thanks for the help!