Problem with player location

Hello,

Just started messing around with Unity.

I managed to make a “player” , make it move, jump, crouch , shoot.
Now I created an enemy and I want the enemy to shoot ( at player location first step).

I don’t know how do I reference my player location to the enemy . In the main scene, my player is an “Object” named let’s say Johnny. This is the sprite i use to move , shoot jump.
Now the question is , when I use a script which has something like this :

"GameObject.FindObjectOfType … " What do I have to input there to reference to Johnny?
If I simply type in there Johnny , it’s an unknown variable for the script to see.

Thx in advance :wink:

if both objects are from the beginn in the scene you can reference it from the editor (ok, maybe not the best solution)

//enemy script
//drag&drop player here in the inspector
public GameObject player;

you can search it with the name (don’t use it, because of slow)

GameObject player;

void Start () {
player = GameObject.Find ("Johnny");
}

give the player tag “Player”, I prefer this

GameObject player;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}

or with the component type (be sure that the other objects don’t have the same component)

GameObject player;

void Start () {
player = FindObjectOfType<NameOfTheScriptOnThePlayer>().gameObject;
}
2 Likes

This is my GameObject Player which says he cant find.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBullet : MonoBehaviour
{

    float moveSpeed = 7f;

    Rigidbody2D rb;

    Player target;
    Vector2 moveDirection;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        target = GameObject.FindObjectOfType<Player>();
        moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
        rb.velocity = new Vector2(moveDirection.x, moveDirection.y);
        Destroy(gameObject, 3f);
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.name.Equals("Player"))
        {
            Debug.Log("Hit!");
            Destroy(gameObject);
        }
    }

}

This is the code for detecting the player.

If I create from scratch a game object, it managed to work for some reason. I don’t understand why I can’t make it work for this Object.

the enemy can’t find the player?

Make:
public GameObject player;
(I hope you have added something to search the player in the scene).

start the game, pause it, switch into the scene tab and check if the enemy has player in the “Player” field (in the inspector) or is it empty.

or show the enemies script

1 Like

target = GameObject.FindObjectOfType();
there is no component “Player” on the player (the components are SpriteRenderer, Animator, CharacterController2D, etc…), also the line can’t find Player on the player and target will stay “null”.

you can try with line:

target = GameObject.Find ("Player");

or better, give the player Tag Player and use:

target = GameObject.FindGameObjectWithTag("Player");
1 Like

Might be best to make

public Transform playerTrans;

Then drag and drop the player in the inspector. Since it’s a public variable, I tend to stay away from using variable names that may exist on other scripts. (Such as player). Also for clarity purposes, it’s a good practice to name variables accordingly. @alex_apolzan

Anyway, since you now posses the player’s coordinates, you would only need to fire at the player’s current position. So maybe make a variable currentPosition = playerTrans.position; then have the enemy fire at that.

private Vector3 currentPosition;

// When player is detected

currentPosition = playerTrans.position;

// You wouldn't want to call this inside an update
// because it would constantly Update the player's
// position. You only want to assign the position
// upon detection