x


Console error says its not a member of component, when it is.

Hi, I have this piece of code:

var enemyScript = hit.transform.GetComponent(scriptEnemy);

    if(hit.transform.tag == tagName){

        //var position = Vector3(Random.Range(-9,9),Random.Range(-4,4), 0);
        //hit.transform.position = position;
        enemyScript.numberOfClicks -= 1;    //reduce the number each click
        //check that the object is at 0 before adding the points to the score
        if (enemyScript.numberOfClicks == 0)
            score += enemyScript.enemyPoint;    //add point to our overall score

    } 

    if (hit.transform.tag == tagName2){

        //var position = Vector3(Random.Range(-9,9),Random.Range(-4,4), 0);
        //hit.transform.position = position;
        enemyScript.numberOfClicks -= 1;    //reduce the number each click
        //check that the object is at 0 before adding the points to the score
        if (enemyScript.numberOfClicks == 0)
            score += enemyScript.enemyPoint;    //add point to our overall score
            Flash();
    } 

When run, the console says "'numberOfClicks' is not a member of 'UnityEngine.Component'. " When it is declared as follows in scriptEnemy out with any functions:

var numberOfClicks  : int           = 2;    //how many times to click on an object before it gets destroyed 

Anyone know whats happening here?

Thanks,

Jack

more ▼

asked Dec 21 '11 at 10:10 PM

JackNutkins gravatar image

JackNutkins
1 1 1 1

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

1 answer: sort voted first

The error is correct--numberOfClicks is, indeed, not a member of Component at all. It's a member of your script. Your script is a Component, but a Component is not your script; that's known as inheritance. I assume you're not using Unity 3.4, because otherwise "var enemyScript = hit.transform.GetComponent(scriptEnemy);" would type enemyScript correctly as scriptEnemy rather than Component. If you're not using Unity 3.4, then you have to cast manually, such as "var enemyScript = hit.transform.GetComponent(scriptEnemy) as scriptEnemy" so you don't end up with Component as the type.

more ▼

answered Dec 21 '11 at 10:34 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

I was wondering if hit.transform.GetComponent would even work, hadn't tried it like that instead of hit.gameObject.GetComponent... so, .transform will work like that too?
What about hit.transform.tag, will that return anything? As well as the tag not being in quotes also? I ask this here rather than making my own question, because it's shown in Jack's script and might be part of the issue also; but I don't know for sure, whereas I'm certain that you do.

Dec 21 '11 at 10:45 PM Lo0NuhtiK

Yes, Transform has tag as an inherited variable and GetComponent as an inherited function. Not sure what you mean about the tag not being in quotes; presumably tagName and tagName2 are strings and you don't put variable names in quotes. (Although CompareTag is preferable to using == comparisons.)

Dec 21 '11 at 11:12 PM Eric5h5

Ok... didn't know transform would work those ways. Thanks.
As for the tag names being in quotes, I didn't see them as variables, so I didn't assume they were declared elsewhere as such. So I was taking 'tagName' and 'tagName2' as being the actual names of the tags, where it would be
if(hit.transform.tag == "tagName")
rather than var tagName : String = "WhateverMyTagIs" ; if(hit.transform.tag == tagName)

Dec 21 '11 at 11:22 PM Lo0NuhtiK
(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:

x5272
x312
x16

asked: Dec 21 '11 at 10:10 PM

Seen: 660 times

Last Updated: Dec 21 '11 at 11:23 PM