Question by
Stonemass · Dec 27, 2020 at 09:33 AM ·
instantiateprefab-instance
Problem instantiating
hi, i try to instantiate a prefab, using clone then, adding component to this clone with addcomponent , inside this component theres some variable, 1 for hp points. but i get always the same F"·%$ error " You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all " So...why ?
this is inside de hpObjects script :
public int vida = 100;
private void Update()
{
if (vida<=0)
{
Destroy(gameObject, 0.2f);
}
}
and this for the instantiate manager :
void Update()
{ spawnPosition = p1.gameObject.transform.position * Random.Range(1, 5);
enemigoEnEscena = GameObject.FindGameObjectWithTag("enemy");
if (enemigoEnEscena == null)
{
prefab.GetComponent<VidaObjetos>();
clon = Instantiate(prefab, spawnPosition, Quaternion.identity);
clon.AddComponent<VidaObjetos>();
}
}
Thanks
Comment