Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Prefab instances with different attributes/unit stats

Discussion in 'Scripting' started by riadsala, Mar 10, 2010.

  1. riadsala


    Feb 10, 2010

    I'm trying to make a little tactical/squad game... a turn based team fortress style game if you will.

    To start with, I've made a prefab which represents a solider. I've tagged this "RedPlayer" and created several instances of the prefab in my screen. Then I created a script which allows you to cycle round all the members on your team. Once a piece is activated, you are allowed to mvoe it for x seconds, where x is a static var in the PlayerStats script which is attached to the prefab.

    This all seems to work fine, EXCEPT... if I try to set each prefab instance to have a different moveTime. (ie, I want some pieces to be able to move for longer than others). The code
    Code (csharp):
    1. currentPiece.GetComponent(PlayerStats).MoveTime
    always seems to return the same value, irrespective of what currentPiece is set to.

    I'm guessing there is a problem with having multiple global variables with the same name? If so, how would I go about defining various stats (hitpoints, accuracy, etc) for each piece?

    From my research so far, I get the impression that I should be defining a class of some sort. However, this doesn't seem to be covered in any of the tutorials (or the Unity Essentials book). If anybody could point me in the right direction, that would be grand.




    The main controller script, attachted to the main camera:
    Code (csharp):
    2. function Update () {
    3.     timetmp = Time.time-lastSwitch;
    4.     // check if we need to switch to the next camera
    5.     if (Input.GetButton ("SwitchCam")  Time.time>(lastSwitch+minSwitchTime))  
    6.     {
    7.         lastSwitch = Time.time;
    8.         // disable control for current piece
    9.         disablePlayerControl();
    10.         // update camera counter
    11.         activePiece = Mathf.Repeat(activePiece+1, friendlyCounter);
    12.         ActivateNewPiece();    
    13.     }
    14.     // if time has run out, switch movement off
    15.     if(Time.time-lastSwitch>currentPieceMoveTime) {
    16.             disablePlayerControl();
    17.     }  
    18. }
    20. function disablePlayerControl() {
    21.     currentPiece.GetComponent(MouseLook).enabled  = false;
    22.     currentPiece.GetComponent(FPSWalker).enabled = false;
    23. }
    25. function ActivateNewPiece() {
    26.     // set current piece
    27.     currentPiece = friendlyPieces[activePiece];
    28.     // move camera
    29.     transform.position = currentPiece.transform.position;
    30.     transform.rotation = currentPiece.transform.rotation;
    31.     transform.parent = currentPiece.transform;
    32.     // enable player control
    33.     currentPiece.GetComponent(MouseLook).enabled = true;
    34.     currentPiece.GetComponent(FPSWalker).enabled = true;
    35.     currentPieceMoveTime = currentPiece.GetComponent(PlayerStats).MoveTime;
    36. }
    The PlayerStats script, attached to each prefab:

    Code (csharp):
    2. static var MoveTime : int;
    3. var moveTime : int = 5;
    5. function Start()
    6. {
    7. MoveTime = moveTime;
    8. }
  2. dbp


    Mar 3, 2010
    you almost answered your own question already. all instances of your object use the same global variable 'MoveTime'. what i think is, that you either misunderstand the Start function or the functionality of global variables.
    the Start function is only called once per lifetime of the object, that is when its loaded. so what happens is that when you load your scene, all the instances of your prefab are trying to write their local 'moveTime' value into the global 'MoveTime' at the same time.
    in your function ActivateNewPiece() the Start() function of the object is not called and therefore the global variable is not updated.

    basically all you have to do is to tell the object to write its local move time into the global one, only when you activate it and not when you load it
  3. riadsala


    Feb 10, 2010

    Guess I misunderstood how global/public variables work in this conext... i thought that the variable would still be attached to the gameobject, so I could have a moveTime for each relevant gameobject in my scene.

    Thanks for the answer :)