Skip to main content
Cleanup
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Unity: Assign GameObject How to Class that hasn't been yet instantiatedassign game object references to a class instance created at runtime?

I'm creating a game where I have an Entity class that contains basic entity info (pos, health,..) and some functions. Enemies and Player later on inherit from this class.

Public class Entity {

public Vector2 position;
public float health;
,...

Lets say that I have a function like this inside the Entity class:

public GameObject movingIntoEnemy(Vector2 movingToPoint)
{
    foreach (GameObject go in GameObject.Find("GameMap").GetComponent<GameMap>().enemyList)
    {
        if (go.GetComponent<EnemyScript>().enemy.position == movingToPoint)
        {
            return go;
        }
    }
    return null;
}

This function looks for a GameObject called "GameMap" and then cycles through enemies in it'sits list. If an enemy is found, it returns it'sits GameObject. Using GameObject.Find() is pretty ineffective, so what would be a better approach to this? I mean, I can't just easily use

public GameObject GameMap;

since the code is in a class, and this class needs to be instantiated first in my player/enemy scripts.

My question is - how do I attach gameObjectsGameObjects to this class before it is even created? What is the appropriate approach to this? I guess that I might pass these GameObjects to the class through it'sits constructor, but it would make things much more complicated.

Thanks in advance ;)

Unity: Assign GameObject to Class that hasn't been yet instantiated

I'm creating a game where I have an Entity class that contains basic entity info (pos, health,..) and some functions. Enemies and Player later on inherit from this class.

Public class Entity {

public Vector2 position;
public float health;
,...

Lets say that I have a function like this inside the Entity class:

public GameObject movingIntoEnemy(Vector2 movingToPoint)
{
    foreach (GameObject go in GameObject.Find("GameMap").GetComponent<GameMap>().enemyList)
    {
        if (go.GetComponent<EnemyScript>().enemy.position == movingToPoint)
        {
            return go;
        }
    }
    return null;
}

This function looks for a GameObject called "GameMap" and then cycles through enemies in it's list. If an enemy is found, it returns it's GameObject. Using GameObject.Find() is pretty ineffective, so what would be a better approach to this? I mean, I can't just easily use

public GameObject GameMap;

since the code is in a class, and this class needs to be instantiated first in my player/enemy scripts.

My question is - how do I attach gameObjects to this class before it is even created? What is the appropriate approach to this? I guess that I might pass these GameObjects to the class through it's constructor, but it would make things much more complicated.

Thanks in advance ;)

How to assign game object references to a class instance created at runtime?

I'm creating a game where I have an Entity class that contains basic entity info (pos, health,..) and some functions. Enemies and Player later on inherit from this class.

Public class Entity {

public Vector2 position;
public float health;
,...

Lets say that I have a function like this inside the Entity class:

public GameObject movingIntoEnemy(Vector2 movingToPoint)
{
    foreach (GameObject go in GameObject.Find("GameMap").GetComponent<GameMap>().enemyList)
    {
        if (go.GetComponent<EnemyScript>().enemy.position == movingToPoint)
        {
            return go;
        }
    }
    return null;
}

This function looks for a GameObject called "GameMap" and then cycles through enemies in its list. If an enemy is found, it returns its GameObject. Using GameObject.Find() is pretty ineffective, so what would be a better approach to this? I mean, I can't just easily use

public GameObject GameMap;

since the code is in a class, and this class needs to be instantiated first in my player/enemy scripts.

My question is - how do I attach GameObjects to this class before it is even created? What is the appropriate approach to this? I guess that I might pass these GameObjects to the class through its constructor, but it would make things much more complicated.

removed the class tag as per http://meta.gamedev.stackexchange.com/questions/2339/whats-the-point-of-the-class-tag
Link
user35344
user35344
Source Link
MSKL
  • 133
  • 3

Unity: Assign GameObject to Class that hasn't been yet instantiated

I'm creating a game where I have an Entity class that contains basic entity info (pos, health,..) and some functions. Enemies and Player later on inherit from this class.

Public class Entity {

public Vector2 position;
public float health;
,...

Lets say that I have a function like this inside the Entity class:

public GameObject movingIntoEnemy(Vector2 movingToPoint)
{
    foreach (GameObject go in GameObject.Find("GameMap").GetComponent<GameMap>().enemyList)
    {
        if (go.GetComponent<EnemyScript>().enemy.position == movingToPoint)
        {
            return go;
        }
    }
    return null;
}

This function looks for a GameObject called "GameMap" and then cycles through enemies in it's list. If an enemy is found, it returns it's GameObject. Using GameObject.Find() is pretty ineffective, so what would be a better approach to this? I mean, I can't just easily use

public GameObject GameMap;

since the code is in a class, and this class needs to be instantiated first in my player/enemy scripts.

My question is - how do I attach gameObjects to this class before it is even created? What is the appropriate approach to this? I guess that I might pass these GameObjects to the class through it's constructor, but it would make things much more complicated.

Thanks in advance ;)