• 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 Zombie_Socks_88 · Dec 03, 2012 at 07:32 AM · javascriptgameobjectprefabmissingreferenceexception

When the original enemy dies, the spawns stop (javascript)

Ok I'm not sure what is going on here. Although I am fairly new to this stuff, so thats most likely the problem. So this is how my project is set up right now. I have a Game Object Cube with tag "enemy" that acts as the enemy, its script is to chase after the player. I also have a Game Object Empty with tag "spawnpoint" that spawns copies of the enemy object.

The problem is that when I kill the original object is stops spawning the enemies from the spawn points and I get a MissingReference Transform Destroyed error. It also happens when if I shot the enemy (it takes 2 shots to kill) and then leave it alone all the spawns only have to be shot once to be killed. I initially thought that all I had to do was create an enemy prefab, but that doesn't work because one of the enemies variables is for searching for the player GameObject, so I can't (more like don't know how) to pass that into the prefab.

So I started thinking it may just be the code that needs to be changed, but I'm not exactly sure what to change to get it to work. Below are my EnemyScript and SpawnScript.

EnemyScript

 var player : GameObject;
 var speed : float = .5;
 public var enemyHealth : int = 2;
     
 function Start()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     //if(!player)
         //Debug.Log("Could Not Find The Player");
 }
 
 function Update()
 {
     if(!player)
         return;
     var distance = Vector3.Distance(player.transform.position, transform.position);
     if(distance < 100)
     {
         //Debug.Log("Player is close by");
         var delta = player.transform.position - transform.position;
         delta.Normalize();
         var moveSpeed = speed * Time.deltaTime;
         transform.position = transform.position + (delta * moveSpeed);
     }
     else
     {
         //Debug.Log("Not close enough" + distance);
     }
     
     if(enemyHealth <= 0)
     {
         Level1InfoScript.playerScore += 100;
         Destroy(gameObject);
     }
 }
 
 function OnCollisionEnter(otherObject : Collision)
 {
     if(otherObject.gameObject.tag == "bullet")
     {
         enemyHealth --;
     }
 }


SpawnScript

 var enemyPrefab: Transform;
 var maxEnemiesAlive: int = 1;
 var maxEnemies: int = 30;
 private var totalEnemies: int;
 private var spawnPoints: GameObject[];
 private var player: Transform;
 
 var fNextSpawn : float = 0.0;
 var fSpawnInterval : float = 5.0;
 var iMaxEnemies : int = 10;
 var iTotalEnemies : int = 0;
 
 function Start()
 {
     fNextSpawn = Time.time + fSpawnInterval;
     
     spawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint");
       player = GameObject.FindWithTag("Player").transform;
       totalEnemies = maxEnemies; // define how many enemies the level has
 }
 
 function Update()
 {
       if(Time.time > fNextSpawn && iTotalEnemies < iMaxEnemies)
        {
         fNextSpawn = Time.time + fSpawnInterval;
         spawnEnemy();
        }
       
       // when some enemy is killed and total enemies isn't zero yet...
       if (transform.childCount < maxEnemiesAlive && totalEnemies > 0)
       {
         // draw a random spawn point:
         var pos = spawnPoints[Random.Range(0, spawnPoints.length)].transform.position;
         // find a rotation to look at the player:
         var rot = Quaternion.LookRotation(player.position - pos);
         // create the new enemy facing the player:
         var enemy = Instantiate(enemyPrefab, pos, rot);
         // child it to Enemies (this object):
         enemy.parent = transform;
         totalEnemies--; // decrement the remaining enemies
     }
 }
 
 function spawnEnemy()
 {
     Instantiate(enemyPrefab, transform.position, Quaternion.identity);
     iTotalEnemies++;
 }
Comment
Add comment
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

2 Replies

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

Answer by vinod.kapoor · Dec 03, 2012 at 07:58 AM

There are two ways as far as I know : First one is to keep the enemy in the hierarchy and de active him by unticking the button in the inspector and then passing it as a variable in the spawn point script as a enemy prefab transform. it will now instantiate the clone of enemy.

this thing has a drawback since the enemy will be loaded at the time of level load and will stay in the game till the level end.

the other way is to make the prefab just like you did earlier and the pass the Player gameObject by writing this in Start function as:

player = GameObject.Find("player name in the heirachy ").transform;

this is better then the previous technique

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 Zombie_Socks_88 · Dec 03, 2012 at 06:10 PM 0
Share

well the code you provided didn't work, I kept getting an error saying "cannot convert UnityEngine.Transform to UnityEngine.GameObject"

However the other part you mentioned did help me out. I created a prefab of the player object, then passed the player prefab into the enemy prefab, then the enemy prefab into the spawn point scene object and deleted the original enemy scene object. So far the only problem I have encountered is that after awhile (and killing all the others) only 1 enemy will be on screen at a time and another wont appear until that one is dead. I thought it was because one of the variables says maxenemiesalive = 1, but I changed it to 5 and it didn't seem to change anything. Other than that, everything is working great. So thanks.

avatar image
0

Answer by vinod.kapoor · Dec 04, 2012 at 05:19 AM

you should have taken the player as transform. then the code would have worked. see the player is currently a GameObject . for this you should have used

//if the player is declared as a GameObject
player = GameObject.Find("player name in the heirachy ")

//if the player is declared as transform player = GameObject.Find("player name in the heirachy ").transform;

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

Prefab problem, wont accept gameObject in var 3 Answers

Destroying a prefab on collision with a cube? 1 Answer

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

Instantiating an Object... 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges