x


error on script getcompnent function

i have to scripts and the first one neads to get a variable value from the second however although the value i want is public it dosent get it it comes up null if i print it and i dont have a clue why

public var curwp : Transform;
public var open : Transform[];
var bestrate : float = 10000;
var val : Transform;

function Start () {
    open = GetComponentsInChildren.<Transform>();
}

function Update () {
    bestrate = 10000;
    for (var val : Transform in open){
        print ("val =" + val);
        var ratesorce = val.gameObject.GetComponents (score);   <---- this
        print (ratesorce);
        print (ratesorce.tlscr);
        if (ratesorce.tlscr < bestrate){
            bestrate = ratesorce.tlscr;
            print (bestrate);
            }
        }
    }

second script

var trg: Transform;
var discr : float;
var mvcst : float;
public var tlscr : float;  <---conects to this... or shold
var cont : GameObject;

function Update () {
curwps = cont.GetComponent (controler);
curwpo = curwps.curwp;
discr = Vector3.Distance(trg.position, transform.position);

if (Vector3.Distance(curwpo.position, transform.position) <= 2.9){
        mvcst = 30;
        transform.renderer.material.color = Color.yellow;
        if (Vector3.Distance(curwpo.position, transform.position) <= 2){
            mvcst -= 10;
            transform.renderer.material.color = Color.green;
        }
    }
    else
    {
        transform.renderer.material.color = Color.red;
        mvcst = 10000;
    }   
tlscr = mvcst + discr;   <---- dose this have anything to do with it
}

any help is aprichiated

more ▼

asked May 09 '11 at 05:53 PM

joseph b gravatar image

joseph b
6 4 8 12

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

1 answer: sort voted first

It's the good old casting problem. GetComponent returns a reference of type Component. You either have to cast it explicit or implicit into the right type.

// the generic version
var ratesorce = val.GetComponents.<score>();
// or implicit casted
var ratesorce : score = val.GetComponents (score);
// or explicit casted
var ratesorce = val.GetComponents (score) as score;

Now ratesorce is of type score and you can access the public members of this class.


edit

I actually don't understand why you use an array of Transform when you just want to access your score script.

That way it's faster and much more reliable. It's the straight forward way:

public var curwp : Transform;
public var open : score[];
var bestrate : float = 10000;

function Start () {
    open = GetComponentsInChildren.<score>();
}

function Update () {
    bestrate = 10000;
    for (var val : score in open){
        if (val.tlscr < bestrate){
            bestrate = val.tlscr;
            print (bestrate);
        }
    }
}
more ▼

answered May 09 '11 at 06:05 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

ime afraid this dident work

May 09 '11 at 07:01 PM joseph b

Are you sure that all of your objects have the score script attached? If you try to get the a component that isn't there you will of course get a null reference.

May 09 '11 at 08:05 PM Bunny83

I have added another solution. I guess your problem is that the GameObject that have this script attached is also returned by GetComponentsInChildren. If you run the game in the editor you should be able see all objects that have been returned in your inspector (since your open array variable is public). if you click on one element it gets highlighted in the hierarchy. I'm sure there is one object that doesn't have the score script attached. I'm pretty sure it's the one with the other script.

May 09 '11 at 08:45 PM Bunny83

thanks that worked and one more question can you get the transform of the scripts game object ??

May 10 '11 at 12:52 PM joseph b

Sure, every component have all the default shorthand properties like .transform, .renderer or .camera. The only one that should always work is .transform because every gameboject have a Transform component. All others can return null when there is no component of that type.

May 10 '11 at 01:32 PM Bunny83
(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:

x3571
x422

asked: May 09 '11 at 05:53 PM

Seen: 344 times

Last Updated: May 09 '11 at 05:53 PM