Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Issue with GetComponent

Discussion in 'Scripting' started by wilhelmscream, Jan 8, 2015.

  1. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    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:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var baseAPop : float = 0;
    4. var baseA : GameObject;
    5. var BasePrefabA : GameObject;
    6.  
    7. function Start () {
    8. }
    9.  
    10. function BuildBase () {
    11.     var baseA : GameObject = Instantiate(BasePrefabA);
    12.     baseA.transform.parent = transform;
    13. }
    14.  
    15. function BuildRegA () {
    16.     BroadcastMessage ("RegA", baseA, SendMessageOptions.RequireReceiver);
    17. }
    18.  
    19. function MilitaryBuild (windowID : int) {
    20.     if (GUI.Button (Rect (5, 91, 90, 20), "BUILD")) {
    21.         InvokeRepeating ("BuildRegA", 10.0, 10.0);
    22.     }
    23. }
    24.  
    25. function Update () {
    26.     baseAPop = baseA.GetComponent(baseScriptA).regPop;
    27. }
    The Update function (GetComponent) is the part that isn't working (or, for that matter, returning any errors).

    Object B:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var regPop : float = 0;
    4. var reg : GameObject;
    5.  
    6. function Start () {
    7. }
    8.  
    9. function RegA () {
    10.     var reg : GameObject = Instantiate(reg);
    11.     reg.transform.parent = transform;
    12.     regPop++;
    13. }
    14.  
    15. function Update () {
    16. }
    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?
     
  2. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    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.
     
  3. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    All those variables are public.
     
  4. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Hmm...When is BuildBase() invoked?
     
  5. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    On command from other buttons I left out, or at Start (randomized).
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Line 11 declares a new variable named baseA. You probably want to assign your class-level baseA instead.
     
  7. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    Would that then be changing
    Code (JavaScript):
    1. function BuildBase () {
    2.     var baseA : GameObject = Instantiate(BasePrefabA);
    3.     baseA.transform.parent = transform;
    4. }
    to
    Code (JavaScript):
    1. function BuildBase () {
    2. Instantiate(baseA);
    3.     baseA.transform.parent = transform;
    4. }
    Or am I misunderstanding something?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    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
     
  9. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    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"
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. baseA = Insantiate(BaseAPrefab);
    3. baseA.transform.parent = transform;
    4.  
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    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.
     
  12. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    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.
     
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    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.
     
  14. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    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)?
     
  15. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Well... what have you got so far? Which part are you having trouble with?
     
  16. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    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.
     
  17. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    This sounds like a rare case where a static variable would actually be handy.
     
  18. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I don't think I've ever used that