- Home /
Prefabs not working properly
So right now I have cannons set up on the left side of the screen and monsters coming from the right. When I place my monsters manually (be 1 or a 1000) "Monster 1 Prefab" on the scene, everything works just fine, the enemies walk towards the cannons, the enemies attack if they are near the cannons and the cannons die if hp <= 0. The problem I am having is that I have an enemy spawn location and a script which generates monsters every x seconds in different locations... this however messes up my monster script somehow because when the monsters get instantiated, they walk towards the cannons as they should but they keep walking when they reach the cannons... and I have no idea why... here is the code for the enemy spawn location:
private int iStartCount = 0;
private const int iEnemySpawnLocTotal = 4;
private GameObject[] goEverySpawnLocation = new GameObject[iEnemySpawnLocTotal];
private const int iMaxEnemiesToSpawn = 10;
public GameObject enemy1;
private randomNumber = 0;
private float fTimer = 0.0f;
private float fSpawnTime = 4.0f;
void Start()
{
goEverySpawnLocation = GameObject.FindGameObjectsWithTag("enemySpawn");
}
Void Update()
{
vRandomPick();
}
void RandomPick()
{
if (iStartCount < iMaxEnemiesToSpawn)
{
fTimer += Time.deltaTime;
if (fTimer > fSpawnTime)
{
randomNumber = Random.Range(0, iEnemySpawnLocTotal);
fTimer = 0;
GameObject myPrefab = (GameObject)Instantiate(enemy1);
myPrefab.transform.position = goEverySpawnLocation[randomNumber].transform.position;
iStartCount++;
}
}
}
I might have few misspelled words, but its because I handwritten this from my other comp so that shouldn't be a problem because it does what it should, the biggest thing I am thinking about is maybe I am instantiating all the enemies as 1 enemy, or that I can't have them as prefabs but have them as actual gameObjects? One other thing to note is that I don't know how to find a gameObject or a prefab in my folder in a script so that is why it is public because I just dragged and dropped it. Maybe it has something to do with my enemy script which I can post but why do the enemies work just fine when I manually place them but not when they get instantiated via script...
Post your enemy-script aswell, since it's the enemies that 'cause you the trouble I'm quite sure that's where the problem is. Are you sure your prefab is set up exactly as the ones you added manually to the scene? If you have values ( as their target for example ) empty in the project inspector and then add your spawn to instantiate that prefab without telling it to set a target, it will spawn with that value as empty aswell. )a
Answer by Ppa0 · Oct 31, 2011 at 10:07 PM
using UnityEngine;
using System.Collections;
public class Monster1 : MonoBehaviour
{
private GameObject Castle;
private float speed = 0.0f;
private float walkingSpeed = 2.5f;
private float iDyingAnimation = 1.1f; //temp, when i have actual animation
private int iDamage = 2;
public int iHealth = 50;
public float attackingTimer = 0.0f;
private float attacksEvery = 0.8f; // animation speed
private Vector3 target;
private float distance = 100.0f;
public bool attacking = false;
private float range = 10.0f;
private GameObject cannon;
void Start ()
{
speed = walkingSpeed;
Castle = GameObject.FindGameObjectWithTag("CastleHitBox");
animation.wrapMode = WrapMode.Loop;
animation.CrossFade("walk");
}
void Update ()
{
transform.Translate(Vector3.right * speed * Time.smoothDeltaTime);
target = Castle.transform.position;
AttackCannon();
Die();
}
void EnemyTakesAHit(int iActualDamage)
{
iHealth -= iActualDamage;
}
void Die()
{
if (iHealth <= 0)
{
animation.CrossFade("dead");
Destroy(gameObject, iDyingAnimation); // temp
speed = 0.0f;
}
}
void AttackCannon()
{
int max = 3;
if (distance < max && cannon != null)
{
speed = 0.0f;
attacking = true;
if (attacking == true)
{
HittingCannon();
}
}
else
{
attacking = false;
distance = 100.0f;
//target = Castle.transform.position;
speed = walkingSpeed;
animation.CrossFade("walk");
}
}
void HittingCannon()
{
Vector3 direction = transform.TransformDirection(Vector3.right);
RaycastHit hit;
attackingTimer += Time.deltaTime;
if ((Physics.Raycast (transform.position, direction, out hit,(float)range)) && attackingTimer > attacksEvery)
{
animation.CrossFade("attack");
cannon.SendMessage("CannonTakesAHit", iDamage);
attackingTimer = 0.0f;
}
}
void OnTriggerStay(Collider col)
{
if (col.tag == "cannon")
{
target = col.transform.position;
cannon = col.collider.gameObject;
distance = Vector3.Distance (target, transform.position);
}
}
}
Unfortunately this is wayyy more complicated than it needs to be, but I just couldn't figure out how to make the enemy attack and access the cannon's life, so I had to make OnTriggerStay, which will probably cause some problems down the road... but anyways, there it is. I know its a lot to ask but if there is a simpler way of doing all of this I would appreciate the example/template/help. Thanks
Your answer
Follow this Question
Related Questions
How to add an asset to a script-enabled public game object? 1 Answer
GameObject change Position after game started 1 Answer
prefab is instantiating without a script 2 Answers
Is it possible to disable/enable game-object inside Instantiate PreFab c# 0 Answers
PlayerRespawn class wont Instantiate the player prefab 1 Answer