#Referencing variables

1 messages · Page 1 of 1 (latest)

calm hatch
#

Hey you still need help with that?

hard plank
#

I do lol

calm hatch
#

Would you mind showing your code?

hard plank
#

This is the script I'm trying to get the object from

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class FindEnemies : MonoBehaviour
{
    private List<GameObject> _enemies = new List<GameObject>();
    public GameObject _closestEnemy;
    public Transform _body;
    [SerializeField] float _interval = 0.25f;

    float _time;
    // Start is called before the first frame update
    void Start()
    {
        _time = 0f;
    }

    // Update is called once per frame
    void Update()
    {
        _time += Time.deltaTime;
        while(_time >= _interval)
        {
            CheckEnemy();
            _time -= _interval;

        }
    }

    void CheckEnemy()
    {
        float _distanceToClosestEnemy = Mathf.Infinity; // Find enemy no matter how far.
        _closestEnemy = null; //Set the current closest to none.
        foreach (GameObject _currentEnemy in _enemies) // For each Enemy assign "currentEnemy" and then run this script.
        {
           float _distancetoEnemy = (_currentEnemy.transform.position - _body.transform.position).sqrMagnitude; //Find the distance of the enemy - our player.
            if (_distancetoEnemy < _distanceToClosestEnemy)
            {
                _distanceToClosestEnemy = _distancetoEnemy;
                _closestEnemy = _currentEnemy;
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            for (int i = 0; i < _enemies.Count; i++)
            {
                if (other.gameObject == _enemies[i])
                {
                    return;
                }
            }
            _enemies.Add(other.gameObject);
            CheckEnemy();
        }
    }
}
#

I removed the OnTriggerExit since it was too long for discord

calm hatch
#

Which line is it you’re having trouble with? I’m on a phone and it’s kinda hard to read

hard plank
#

So I'm just not entirely sure how to get the GameObject _closestEnemy
from this script to another

#

I have it set as public GameObject _closestEnemy;
and then in script 2 I've tried this

    FindEnemies _findEnemies;

    // Start is called before the first frame update
    void Start()
    {

    }

    private void Awake()
    {
        _findEnemies = GetComponent<FindEnemies>();
    }

    // Update is called once per frame
    void Update()
    {
        _closestEnemy = _findEnemies._closestEnemy;
        Debug.Log(_closestEnemy);
        FindClosestEnemy(); //Find the closest enemy every frame. 

    }```
#

I've tried a whole bunch of different ways and it always comes back as null

calm hatch
#

So is your script where you want to get the find enemies component from on the same game object as the second script?

hard plank
#

It's not
I have the player and then another object attached to the player which atm is just a giant collider (using as trigger) and the FindEnemies script

#

and the 2nd script that I'm trying to get the _closestEnemy to is on the player

calm hatch
#

Well then you have to get the gameobject first

#

FindEnemies _findEnemies = GameObject.Find(„Name of gameobj with the script on it“).GetComponent<FindEnemies>();

hard plank
#

It makes sense to me now and I understand what I was doing wrong

#

thanks a lot 🙂

calm hatch
#

Lol