Issue with GetComponent

This explanation may be over-complicated so bear with me.

I have 3 objects - A instantiates B as a child, B instantiates C as a child, A has a float variable that needs to be the number of C objects attached to B.

The codes for instantiation all work properly (everything is being properly cloned and set as children). B is properly counting it’s C objects in a float value. My issue is, using GetComponent on that float, A is not reading B at all (using a code string identical in setup to others that work fine.)

Here are the relevant code snippets…

Object A:

#pragma strict

var baseAPop : float = 0;
var baseA : GameObject;
var BasePrefabA : GameObject;

function Start () {
}

function BuildBase () {
    var baseA : GameObject = Instantiate(BasePrefabA);
    baseA.transform.parent = transform;
}

function BuildRegA () {
    BroadcastMessage ("RegA", baseA, SendMessageOptions.RequireReceiver);
}

function MilitaryBuild (windowID : int) {
    if (GUI.Button (Rect (5, 91, 90, 20), "BUILD")) {
        InvokeRepeating ("BuildRegA", 10.0, 10.0);
    }
}

function Update () {
    baseAPop = baseA.GetComponent(baseScriptA).regPop;
}

The Update function (GetComponent) is the part that isn’t working (or, for that matter, returning any errors).

Object B:

#pragma strict

var regPop : float = 0;
var reg : GameObject;

function Start () {
}

function RegA () {
    var reg : GameObject = Instantiate(reg);
    reg.transform.parent = transform;
    regPop++;
}

function Update () {
}

The Object B variable “regPop” should be read by A to get baseAPop… but baseAPop constantly remains at zero no matter what regPop shows.

Thoughts?

I am not a UnityScript expert I script in C# but, is it possible the variable is not visible? Try making it public. If that doesn’t work let me know.

All those variables are public.

Hmm…When is BuildBase() invoked?

On command from other buttons I left out, or at Start (randomized).

Line 11 declares a new variable named baseA. You probably want to assign your class-level baseA instead.

Would that then be changing

function BuildBase () {
    var baseA : GameObject = Instantiate(BasePrefabA);
    baseA.transform.parent = transform;
}

to

function BuildBase () {
Instantiate(baseA);
    baseA.transform.parent = transform;
}

Or am I misunderstanding something?

Yes, what you’re misunderstanding is that using “var” creates a new local variable, which is different from the global variable you declared on line 4. Remove the “var”.

–Eric

It created the prefabs but … didn’t set any of them as children of A, and generated this message:

“Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption”

baseA = Insantiate(BaseAPrefab);
baseA.transform.parent = transform;

Yep, you’re attempting to modify the prefab rather than the spawned instance.

What do you mean by this? Since the float data type is an approximation it’s not typically a good candidate for storing discreet values, which is what you typically want to do when counting something.

Thanks Kelso! Got it working fine now. The objective is to find a way of serializing (probably the wrong word but whatever) unit identification numbers (i.e. 1st Regiment, 2nd Regiment, etc) without having to individually name them.

I think an int would be better for that than for a float. For starters, you’re only using whole numbers - you’ll never have the 1.5th Regiment, or the 9.999th Regiment. Secondly, you can reliably check for equality with an int, which you can’t do with a float, which helps you do things like detect post-fixes (1st, 2nd, 3rd, 4th) far more simply.

You know of any good/clean/straightforward ways of generating that kind of sequencing? I.e, where each of a unit type is ID’d in a sequential value (1st, 2nd, 3rd, etc)?

Well… what have you got so far? Which part are you having trouble with?

I’m not having trouble per se just wondering if there’s a simpler way.

As of now, each regiment on creation sends a message to a master script which doesn’t get destroyed. That message causes an int variable to add 1, which is then read by the regiment (once) and used as the regiment’s ID.

This sounds like a rare case where a static variable would actually be handy.

I don’t think I’ve ever used that