- Home /
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:
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);
}
}
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?
Exactly.
Currently giving a result proyect using find() results in an F because of computational drag. $$anonymous$$y $$anonymous$$cher won't always answer me how to solve troubles and this problem is goiing to be very recurrent I bet.
Glad to know I stumbled into something interesting tho.
Answer by Minchuilla · Apr 19, 2014 at 03:53 PM
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
Thanks man you helped me. The enemy-prefab has a children with a component. I have tried it with:
spawnObject.GetComponentInChildren<stomp> ().controller = GameObject.Find ("Player");
but they don't assign it. How should I do that?
Is there a alternative to GameObject.Find(), because I have heard that it's slow.
Sorry, could you just clarify what you are trying to do and what isn't working.
Your code should work as long as your variable "controller" is of the type GameObject.
As far as I know GameObject.Find isn't slow and shouldn't prevent your program from working.
$$anonymous$$inchuilla
I instantiate a enemy-prefab. Now the enemy has a child. The child(squishybox) has a script-component. I try to assign to the child-script a gameobject with GetComponentInChildren<>() but they don't assign them.
I recommend that you have a look at the docs below:
GameObject.Find:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
How to assign things:
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
You could also try this to get the variable but this isn't recommended:
spawnObject.GetChild(0).GetComponent<stomp>().controller
But if you are trying to get a prefab with GameObject.Find this will not work as it will only look for objects within the scene that are already instances(it will only check through the objects in the Hierarchy)
If you want to assign your variable a prefab you need to use:
Resources.Load("folder/object");
Another post explains this:
http://answers.unity3d.com/questions/29876/how-can-i-assign-prefab-to-variable-without-drag-d.html
In your case I would do this:
spawnObject.GetComponentInChildren<stomp> ().controller = Resources.Load ("PlayersFolderLocation/Player");
You are the best thanks for everything, I appreciate it.
I have used this code:
spawnObject.transform.GetChild (0).GetComponent<stomp> ().controller = GameObject.Find ("Player");
Answer by OrbitSoft · Apr 18, 2014 at 09:44 PM
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;
I tried to do with this:
void Spawn()
{
if(enemyCount <= 1)
{
enemyCount = 2;
Instantiate(spawnObject,spawnPosition.position,Quaternion.identity);
spawnObject.GetComponent<EnemyHealthController>().enemycount = GameObject.Find("EnemySpawn");
}
}
But i got this error:
Assets/Scripts/EnemyCount.cs(31,75): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `EnemyCount'
EnemyCount.cs Script: using UnityEngine; using System.Collections;
public class EnemyCount : $$anonymous$$onoBehaviour {
public int enemyCount = 2;
public GameObject spawnObject;
public Transform spawnPosition;
public HealthController healthController;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(enemyCount <= 1 && healthController.respawn)
{
Spawn();
}
}
void Spawn()
{
if(enemyCount <= 1)
{
enemyCount = 2;
Instantiate(spawnObject,spawnPosition.position,Quaternion.identity);
spawnObject.GetComponent<EnemyHealthController>().enemycount = GameObject.Find("EnemySpawn");
}
}
}
void Spawn()
{
if(enemyCount <= 1)
{
enemyCount = 2;
GameObject newEnemy = (GameObject)Instantiate(spawnObject, spawnPosition.position, Quaternion.identity);
newEnemy.GetComponent<EnemyHealthController>().enemycount = GameObject.Find("EnemySpawn");
}
}
Is the same error:
Assets/Scripts/EnemyCount.cs(33,72): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `EnemyCount'
You cannot assign a gameobject to enemy count! It's not a gameobject variable. You can only assign an EnemyCount to it.
If your gameobject has an EnemyCount component then you have to Gameobject.Find("enemyspawn").GetComponent()
a gameobject called "EnemySpawn" has the EnemyCount component.
That means there is no solution for this, right?