x


FindWithTag error

Hi everyone, I am trying to use FindWithTag to search for all of the enemies on the screen with tag "enemy". The script works fine and it finds all of them, however if all enemies are destroyed, the script sends me an error message saying "NullReference...", and I have no idea how to fix it :(. I did try using GameObject instead of Transform and it works fine, however later in the script I am trying to use the LookAt which needs "Transform" and gives me error. It doesn't matter which way I go, it will give me an error, which I cant fix myself. Do you have any idea how to fix it? The code:

var target : Transform;
var boltSpeed : float = 10;
function Update() 
{
if(!target)
{
    var TempSearch = GameObject.FindWithTag("enemy").transform;
    if(!TempSearch) 
    { 
       Destroy(gameObject, 0.01);
       print("dead");
    }
    else if(TempSearch)
    {
       target = TempSearch;
    }
}
else if(target)
    {
       transform.LookAt(target);
       if(gameObject.transform.position != target.transform.position)
       {
         gameObject.transform.Translate(Vector3.forward * boltSpeed * Time.deltaTime);
       } 
    }
}

The error message:

Uploaded with ImageShack.us

more ▼

asked Aug 23 '11 at 09:55 PM

warkarma gravatar image

warkarma
31 1 1 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest
var TempSearch = GameObject.FindWithTag("enemy").transform;

Isn't Right, Use this:

var target : GameObject; var boltSpeed : float = 10; function Update() { if(!target) { var TempSearch = GameObject.FindWithTag("enemy"); if(!TempSearch) { Destroy(gameObject, 0.01); print("dead"); } else if(TempSearch) { target = TempSearch; } } else if(target) { transform.LookAt(target.transform); if(gameObject.transform.position != target.transform.position) { gameObject.transform.Translate(Vector3.forward * boltSpeed * Time.deltaTime); } } }

more ▼

answered Aug 23 '11 at 10:33 PM

FTheCloud gravatar image

FTheCloud
735 15 20 27

I got now a new error

Uploaded with ImageShack.us

Aug 23 '11 at 11:46 PM warkarma

Assumed that you were using transform in your script since you had .transform at the end. Just take out the : Transform. Edited my answer.

Aug 23 '11 at 11:48 PM FTheCloud

Same error

Aug 24 '11 at 12:06 AM warkarma

Oh I see The Problem. Its when you say target = tempSearch. You cant do that because you're defining the var target as Transform and the var tempSearch as a GameObject. They can't equal eachother because there not the same type. Also at the lookat function, thats when you use the .transform. Changed my answer again, again to show you what it should look like.

Aug 24 '11 at 12:28 AM FTheCloud

Yey, it works fine now :) Thanks a lot

Aug 24 '11 at 06:09 AM warkarma
(comments are locked)
10|3000 characters needed characters left

╭━━灬╮╭━━∞╮  ┃⌒ ⌒┃┃⌒ ⌒┃  ╰━━━〇〇━━━〇 Looking forward to your visiting

input this URL: ★ http://www.goodshopping100.com

The website wholesale for many kinds of fashion shoes, like the nike, jordan, prada, also including the jeans, shirts, bags, hat and the decorations.

All the products are free shipping, and the the price is competitive, and also can accept the paypal payment., After the payment, can ship within short time.

free shipping

competitive price

any size available

accept the pyapal

╭══════════════╮

http://www.goodshopping100.com

╰══════════════╯

exquisite watches 75$

90X Extreme Fitness System ONLY ONLY 42 $$$$$$$

jordan shoes $ 32

nike shox $ 32

Christan Audigier bikini $ 23

Ed Hardy Bikini $ 23

Sm ful short_t-shirt_woman $ 15

ed hardy short_tank_woman $ 16

Sandal $ 32

christian louboutin $ 80

Sunglass $ 15

COACH_Necklace $ 27

handbag $ 33

AF tank woman $ 17

puma slipper woman $ 30

90X Extreme Fitness System ONLY ONLY 42 $$$$$$$

Believe you will love it.

we will give you a big discount

Opportunity knocks but once

welcome to: ==== http://www.goodshopping100.com ====

more ▼

answered Aug 24 '11 at 12:13 AM

xiangkoe (suspended)
(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:

x2171
x337
x33

asked: Aug 23 '11 at 09:55 PM

Seen: 547 times

Last Updated: Aug 24 '11 at 06:09 AM