x


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...

more ▼

asked Oct 31 '11 at 05:06 PM

Ppa0 gravatar image

Ppa0
16 8 8 10

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

Oct 31 '11 at 07:07 PM Kona
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
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

more ▼

answered Oct 31 '11 at 10:07 PM

Ppa0 gravatar image

Ppa0
16 8 8 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2090
x1677
x1259
x72

asked: Oct 31 '11 at 05:06 PM

Seen: 751 times

Last Updated: Oct 31 '11 at 10:07 PM