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 Celsius · Oct 29, 2012 at 12:19 AM · prefabenemyspawn

Spawning enemy mobs .......... Instantiate woe ?

Hi guys, my second question here.

"Spawning enemy mobs. the enemy works fine by itself (as a prefab) but not when it's spawned using this code (instantiate)?"

I have an enemy, which, if I use by itself, works 100%. When I say use by itself, I mean dragging and dropping it on to the game world. However, I wish to instantiate it using the below code, to spawn it whenever the player enters. Now, it does spawn the enemy I want but a major problem occured.

The player is unable to attack the instantiated enemy. the instantiated enemy is working well on it's side, it chases the player and can apply damage. The player can only do it to the normal enemies, not the ones instantiated. Below are two codes. One code is in Javascript, from the lerpz tutorial. It works well in spawning enemies but I am unfamiliar with javascript compared to c#. If it helps, all my other codes are in C#. The one below the lerpz code is my own code. It's mainly for testing purposes.

Both codes spawn enemies well and the enemies interact well with the player. Neither codes allows the player to interact with the enemy. My attack code for player works 100% otherwise and can be found here via the selected answer.

Lerpz enemy respawn code: /* EnemyRespawn.js

     This script checks if the player is in range. If so, it instantiates the enemy prefab specified. When the player moves out of range, the prefab is automatically destroyed.
     
     This prevents repeated calls to the enemy's AI scripts when the AI is nowhere near the player, this improving performance.
 
 */
 
 
 var spawnRange = 0.0;    // the distance within which the enemy should be active.
 var gizmoName : String;        // the type of the object. (See OnDrawGizmos() for more.)
 var park : GameObject;    // link to the Prefab we'll be instantiating / destroying on demand.
 
 // Cache variables, used to speed up the code.
 private var player : Transform;
 private var currentEnemy : GameObject;
 private var wasOutside = true;
 
 // Called on Scene startup. Cache a link to the Player object.
 // (Uses the tagging system to locate him.)
 function Start ()
 {
     player = GameObject.FindWithTag("Player").transform;
 }
 
 // Called at least once every game cycle. This is where the fun stuff happens.
 function Update ()
 {
     // how far away is the player?
     var distanceToPlayer = Vector3.Distance(transform.position, player.position);
 
     // is he in range?
     if (distanceToPlayer < spawnRange)
     {    
         // in range. Do we have an active enemy and the player has just come into range, instantiate the prefab at our location. 
         if (!currentEnemy && wasOutside)
             currentEnemy = Instantiate(park, transform.position, transform.rotation);
         
         // player is now inside our range, so set the flag to prevent repeatedly instantiating the prefab.
         wasOutside = false;
     }
     // player is out of range.
     else
     {    
         // is player leaving the sphere of influence while our prefab is active?
         if (currentEnemy && !wasOutside)
             Destroy(currentEnemy);    // kill the prefab...
 
         // ...and set our flag so we re-instantiate the prefab if the player returns.
         wasOutside = true;
     }
 }
 
 // Called by the Unity Editor GUI every update cycle.
 // Draws an icon at our transform's location. The icon's filename is derived from the "type" variable, which allows this script to be used for any enemy.
 function OnDrawGizmos ()
 {
     Gizmos.color = Color(1, 1, 1, 1);
     
     // See the help docs for info on where the icon needs to be stored for this function to work.
     Gizmos.DrawIcon(transform.position, gizmoName + ".psd");    
 }
 
 // Called by the Unity Editor GUI every update cycle, but only when the object is selected.
 // Draws a sphere showing spawnRange's setting visually.
 function OnDrawGizmosSelected ()
 {
     Gizmos.color = Color(0, 1, 1);
     Gizmos.DrawWireSphere(transform.position, spawnRange);
 }


my code for testing: using UnityEngine; using System.Collections;

 public class respawn : MonoBehaviour {
 
 public GameObject target;
 public GameObject target2;
     
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
     float distance = Vector3.Distance(target.transform.position, transform.position);
 
     if(distance<20){
         if(Input.GetKeyUp(KeyCode.Z)) {
            Instantiate(target2 as GameObject);
 
         }        
         }
 
     if(distance>100)
         Destroy(target2);
     }        
 }


any help is very much appreciated. It's quite confusing when the prefab itself works but when I instantiate it as above, the player stops recognizing the very same prefab.

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 Fattie · Oct 29, 2012 at 08:26 AM 0
Share

Celsius, you may have to learn how to have a pool of objects in video gamed development. You'll have to learn this eventually so it may be time!

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

as a broad general rule, you never Instantiate anything during gameplay in a vid game. Hope the link helps!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by podperson · Oct 29, 2012 at 12:47 AM

I suspect your code has little to do with it. How is the player supposed to damage the enemy? (This is the problem, right?) My guess is it's caused by collisions or triggers, and chances are the instantiation is creating something in the wrong layer and is being ignored by the collision detection code. Or perhaps your prefab is missing the collider.

Comment
Add comment · Show 1 · 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
avatar image Celsius · Oct 29, 2012 at 04:43 AM 0
Share

could you please explain the part about the prefab not having any variables that are assigned to an object in the scene through the inspector?

because I do not have nor did I though I need that. I'm very much a beginner in unity.

avatar image
0

Answer by TheDarkVoid · Oct 29, 2012 at 12:42 AM

The script on the prefab doesn't save any variables that are assigned to an object in the scene though the inspector.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy and Spawn an Enemy 1 Answer

Error: Instantiated Enemies don't get hit 2 Answers

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

Enemy not spawning correctly 1 Answer

Problem with random spawning and coroutine 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