Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ppa0 · Oct 31, 2011 at 05:06 PM · gameobjectinstantiateprefabfolder

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kona · Oct 31, 2011 at 07:07 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges