How to assign GameObject to a instantiated prefab via Script(C#)?

I have a enemy-prefab with a EnemyHealthController. In the EnemyHealhController there is a public Script-Variables for two different GameObjects.
Now when I instantiate a new enemy-prefab to the current scene, the variables are unassigned. I would that the
variable should assigned to the Player in the current scene without drag and drop or select. Is there a solution to solve this problem?

From this:

25400-unbenannt.png

to this:

The public Scripts-Variables are:

public EnemyCount enemycount;
public ExpSystem expSystem;

EnemyHealthController:

using UnityEngine;
    using System.Collections;
     
    public class EnemyHealthController : MonoBehaviour {
     
            public float currentHealth = 2;
            public float DamageEffectPause = 0.2F;

            public ExpSystem expSystem;
            public EnemyCount enemycount;
     
            void ApplyDamage(float damage)
            {
                   
                    if (currentHealth > 0)
                    {
                           
                            currentHealth -= damage;
                           
                            if (currentHealth <= 0)
                            {
                                    currentHealth = 0;                     
                                    Die();
                            }      
                            else
                            {
                                    StartCoroutine(DamageEffect());
                            }
                    }
            }
     
            IEnumerator DamageEffect()
            {
                    renderer.enabled = false;
                    yield return new WaitForSeconds(DamageEffectPause);
                    renderer.enabled = true;
                    yield return new WaitForSeconds(DamageEffectPause);
                    renderer.enabled = false;
                    yield return new WaitForSeconds(DamageEffectPause);
                    renderer.enabled = true;
                    yield return new WaitForSeconds(DamageEffectPause);
                    renderer.enabled = true;
            }
     
            void Die()
            {
                    expSystem.curEXP += 10;
                    enemycount.enemyCount--;
                    Destroy (gameObject);
            }
    }

The Instantiate function returns the object you are instantiating so you can do the following:

GameObject newEnemy = (GameObject)Instantiate(enemy,stuff);

Now you can acces its data normally:

newEenemy.GetComponent<EnemyHealthController>().enemycount = someThing;

Firstly, you say that your public script variables are GameObject but you your code defines them as the type of EnemyCount, which I don’t think is a type unless you have a class named after it.

So what you need to do is this:

-change you variables to this

public GameObject enemycount;
public GameObject expSystem;

-to get the GameObject in code do as you have done in the comments

newEnemy.GetComponent<EnemyHealthController>().enemycount = GameObject.Find("name of GameObject");

-if you still get an error with this line then you might have to cast it although I don’t think that would make a difference as GameObject.Find returns a GameObject

i.e.

newEnemy.GetComponent<EnemyHealthController>().enemycount = (GameObject)GameObject.Find("EnemySpawn");

Hope you problem is solved and feel free ask more questions

Minchuilla

Hey Guys,
Is there a better way other than using find? as find goes through the entire gameobjects in the scene and is a computationally expensive?