Search Unity

Copying the value of instant of other instant variable with Random.Range make unexpected behavior?

Discussion in 'Scripting' started by Ultimate360, Jul 25, 2017.

  1. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    I make a simple script for testing...
    I attach a zip file...
    The bulletPattern.respawnOffset changes over time even it's for patternApply.respawnOffset, I mean I don't even initialize the bulletPattern.respawnOffset, but it changes. It's hard to explain, just see and test the script...
    I want to know why this happen?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class TransformPattern {
    6.     public GameObject bulletPrefab;
    7.     [Header("Bullet Patterns:")]
    8.     public Vector2 respawnOffset;
    9.     public Vector2 masterOffset;
    10.     public string stringVar;
    11.     //if I have more List with List...
    12. }
    13.  
    14. public class RandomRespawnTest : MonoBehaviour {
    15.     [Header("Bullet Transform:")]
    16.     public TransformPattern bulletPattern = new TransformPattern();
    17.     void Update () {
    18.         startRespawn ();
    19.     }
    20.  
    21.     void startRespawn ()
    22.     {
    23.         TransformPattern patternApply = new TransformPattern();
    24.         /*
    25.             //Imagine, if I have many variable in TransformPattern... Instead of doing this, individually:
    26.                 patternApply.masterOffset = bulletPattern.masterOffset;
    27.                 patternApply.stringVar = bulletPattern.stringVar;
    28.                 patternApply.otherVar_1 = bulletPattern.otherVar_1;
    29.                 patternApply.otherVar_2 = bulletPattern.otherVar_2;
    30.                 patternApply.otherVar_3 = bulletPattern.otherVar_3;
    31.             I do below, and successfully copy everything, but there's a problem:
    32.         */
    33.    
    34.         /* Take one of these three line by commenting other two out by // to see results */
    35.  
    36.             //patternApply.masterOffset = bulletPattern.masterOffset;
    37.             //patternApply.masterOffset = bulletPattern.masterOffset; patternApply.stringVar = bulletPattern.stringVar;
    38.             patternApply = bulletPattern; /* Initialize patternApply... Problem manifest here... why? It's weird? Why the bulletPattern manipulated? Even nothing touches it or I initialize it? 'patternApply' and 'bulletPattern' are two different variables, right? */
    39.  
    40.         /* ^ Swap these three to see results: */
    41.  
    42.         if(patternApply.stringVar == null || patternApply.stringVar == "")
    43.             patternApply.stringVar = "Other Variable, SEE?";
    44.    
    45.         //Get the random generated value range from bulletPattern.respawnOffset for patternApply.respawnOffset...
    46.         float respawnOffsetX = Random.Range (-bulletPattern.respawnOffset.x, bulletPattern.respawnOffset.x);
    47.         float respawnOffsetY = Random.Range (-bulletPattern.respawnOffset.y, bulletPattern.respawnOffset.y);
    48.         Debug.Log ("bulletPattern.stringVar: "+bulletPattern.stringVar+" | patternApply.stringVar: "+patternApply.stringVar);
    49.  
    50.         patternApply.respawnOffset = new Vector2 (respawnOffsetX, respawnOffsetY);
    51.  
    52.         Vector3 respawnOffset_V3 = new Vector3 (patternApply.respawnOffset.x, patternApply.respawnOffset.y, 0f);
    53.         Vector3 masterOffset_V3 = new Vector3 (patternApply.masterOffset.x, patternApply.masterOffset.y, 0f);
    54.         Vector3 transformPosition = transform.position + respawnOffset_V3 + masterOffset_V3;
    55.         //Debug.Log (transformPosition);
    56.  
    57.         Instantiate (patternApply.bulletPrefab, transformPosition, transform.rotation);
    58.     }
    59. }
    This is my real script I'm working on, this is kind of pain if I initialize all (some) of this:
    (Disregard this, for illustration purpose only)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class BulletModification {
    7.     [Header("Modify Cluster:")]
    8.     public float startSpeed;
    9.     public enum generateAcceleration {
    10.         byUnit,
    11.         byPercentage
    12.     } public generateAcceleration accelerationType;
    13.     public float acceleration;
    14.     public float speedRange;
    15. }
    16.  
    17. [System.Serializable]
    18. public class TransformFormula {
    19.     //[Header("Stir Bullet:")]
    20.     [Header("Mirror Bullet:")]
    21.     [Tooltip("0 means all copies will be on sync, beyond that will make delay offset speed; 100 makes the last delay copy to stationary, 200 makes go to opposite direction. Set delayAmount above 0 to see result")]
    22.     [Range(0,200)] public float speedOffset;
    23.     [Tooltip("Number of clone bullets for offset speed. Set speedOffset to work with")]
    24.     [Range(0,3)] public int delayAmount;
    25.     [Tooltip("Number of clone bullets shoot parallel to each other")]
    26.     [Range(1,128)] public int linearAmount = 1;
    27.     public enum spaceType {
    28.         byUnitAway,
    29.         byOverallWidth
    30.     }; [Tooltip("How parallel bullets take up space")] public spaceType areaOccupy;
    31.     [Tooltip("The amount of parallel bullets away each other or their general width as one group")]
    32.     public float spacingValue;
    33.     [Tooltip("Number in seconds; parallel from the middle will start immediately, then delay start to the next neighbor line")]
    34.     [Range(0,20)] public float delayLinear;
    35.  
    36.     [Header("Spray Bullet:")]
    37.     [Tooltip("The degree angle of an arc be made for rayAmount")]
    38.     [Range(0,360)] public float spreadAngle;
    39.     [Tooltip("The number of copy bullet as rays restricted by spreadAngle")]
    40.     [Range(1,32)] public int rayAmount = 1;
    41.     [Tooltip("The radius offset of the center of the ray")]
    42.     public float spawnOffsetRadius;
    43.     [Tooltip("Number in seconds; the first ray will start immediately, then delay start to the next neighbor ray")]
    44.     [Range(0,20)] public float delayRay;
    45. }
    46.  
    47. [System.Serializable]
    48. public class TransformPattern {
    49.     [Header("Bullet Patterns:")]
    50.     public Vector2 respawnOffset;
    51.     [Tooltip("Sliding the respawn origin of bullet sideward from left to right length-based over given duration")]
    52.     public float linearMagnitude;
    53.     public enum linearMotionType{
    54.         Linear,
    55.         Wave
    56.     }; public linearMotionType linearFlow;
    57.     public enum linearDirection{
    58.         left_Right,
    59.         right_Left,
    60.         center_Right,
    61.         center_Left
    62.     }; public linearDirection linearPoint;
    63.     [Tooltip("Move with one direction or play ping pong")]
    64.     public bool pingPong = true;
    65.     [Tooltip("The spin progress over given duration; Note: clinch with 'Linear Magnitude'")]
    66.     public float spinMotion;
    67.     public enum spinMotionType {
    68.         Linear,
    69.         Wave
    70.     }; public spinMotionType spinFlow;
    71.     public enum spinDirection{
    72.         left_Right,
    73.         right_Left,
    74.         center_Right,
    75.         center_Left
    76.     }; public spinDirection spinPoint;
    77.     [Tooltip("Merry-go-around or not")]
    78.     public bool swingBack = false;
    79.     [Tooltip("As it says; offset vertically and/or horizontally -_-")]
    80.     public Vector2 masterOffset;
    81.     [Tooltip("Offset overall rotation with parent object, but only in z-axis, any problem with that?! Note: doesn't spin over time, use spinMotion or bullet formula instead, or use emitter and make it spin via other script (or animation?)")]
    82.     public float masterRotation;
    83.     [Tooltip("Offset rotation disregarding linear orientation")]
    84.     public float offsetRotation;
    85. }
    86.  
    87. [System.Serializable]
    88. public class TimeLine {
    89.     [Header("Bullet Time:")]
    90.     [Tooltip("Ready to execute one time once timeCode reaches")]
    91.     public bool active = true;
    92.     [Tooltip("Time in seconds to start the bullet party")]
    93.     public float start;
    94.     [Tooltip("Time in seconds to finish one pattern")]
    95.     public float duration;
    96.     [Tooltip("One more duration to repeat the pattern")]
    97.     public int repeat;
    98.     [Tooltip("Number of bullet emits in one span of duration harmonizing with pattern; Note: minimum fire rate will not less than 0.1 sec, or else actionPulse will be deducted")]
    99.     public int actionPulse;
    100.  
    101.     [Header("Bullet Element:")]
    102.     [Tooltip("To certainly choose none, input less than 0")]
    103.     public int pattern;
    104.     [Tooltip("To certainly choose none, input less than 0")]
    105.     public int formula;
    106.     [Tooltip("To certainly choose none, input less than 0")]
    107.     public int modification;
    108. }
    109.  
    110. [System.Serializable]
    111. public class BulletSpawn {
    112.     [Tooltip("Your title, definition, description, speech, message, or whatever; :Optional:")]
    113.     public string name;
    114.     public GameObject bulletPrefab;
    115.     [Tooltip("Spawn point: object attached with this script act as default emitter, if leave empty")]
    116.     public List<GameObject> emitter;
    117.     public List<TimeLine> bulletTime;
    118. }
    119.  
    120. [System.Serializable]
    121. public class Bulletflow {
    122.     /* Note: for the future viewer; why no 'public bool active'?
    123.      * No need, I found it useless; use/code timeHalt + reset = inactive (if you want to extend this code)... */
    124.     public bool reset;
    125.     public bool timeHalt;
    126.     public bool looping;
    127.     [Tooltip("Set loopQuantity to zero for infinite loop")]
    128.     public int loopQuantity;
    129.     [Tooltip("What for? Delay the start time of timeCode")]
    130.     public float delayStart;
    131.     [Tooltip("Behold the time holder! NOLI ME TANGERE, if you don't know me what I am for")]
    132.     public float timeCode;
    133. }
    134.  
    135. public class HellBulletManager_New : MonoBehaviour {
    136.  
    137.     [Header("Bullet Manager:")]
    138.     public Bulletflow bulletRush;
    139.     public List<BulletSpawn> hellBullets;
    140.  
    141.     [Header("Bullet Transform:")]
    142.     public List<TransformPattern> bulletPattern = new List<TransformPattern> ();
    143.  
    144.     [Header("Bullet Properties:")]
    145.     public List<TransformFormula> bulletFormula;
    146.     public List<BulletModification> bulletModification;
    147. }
     

    Attached Files: