Search Unity

Bug Trying to separate variables of separate instances.

Discussion in 'Scripting' started by Jakets, Apr 17, 2023.

  1. Jakets

    Jakets

    Joined:
    Nov 23, 2020
    Posts:
    1
    I am instantiate objects every-time a key is pressed and changing its size based on variables. Every time I press another key to instantiate a new object, it effects the size of the object that has already been instantiated.

    The objects only stay on screen as long as same key is held down and destroyed when the same key is released. Each object is supposed to resize separately but they resize based on the lates morph that is cloned. They also all get destroyed after any key is released even if their original key is held down.

    This is the function that instantiates the object.
    Code (CSharp):
    1. public GameObject InstantiateMorph(Vector3 spawnPosition)
    2.     {
    3.         // create a new object, set it to active (prefab is unactive)
    4.         GameObject morphClone = Instantiate(morphObject, spawnPosition, Quaternion.identity);
    5.         morphClone.SetActive(true);
    6.  
    7.         // add the morph clone to an array, get the latest morph clone in the array
    8.         morphClones.Add(morphClone);
    9.         latestMorphCloneIndex = morphClones.Count - 1;
    10.         latestMorphClone = morphClones[latestMorphCloneIndex];
    11.  
    12.         Debug.Log("latest morphe index" + latestMorphCloneIndex);
    13.         //latestMorphClone.transform.localScale = Vector3.one * 0;
    14.  
    15.         // get the scripts from the latest morph
    16.         MorphSizeController morphSizeController = latestMorphClone.AddComponent<MorphSizeController>(); // Add MorphSizeController component
    17.         SizeController2 sizeController = latestMorphClone.GetComponent<SizeController2>();
    18.  
    19.         // set the ID of the scripts to the _idCounter variable and increase the variable +1
    20.         sizeController.targetID = _idCounter++;
    21.         morphSizeController.ID = sizeController.targetID;
    22.         sizeController.tag = sizeController.targetID.ToString();
    23.  
    24.         return morphClone;
    25.     }
    These are the coroutines that control the object size and remove it after the lerps:

    Code (CSharp):
    1. public IEnumerator PerformLerpChain(KeyCode keyPressed)
    2.     {
    3.         MorphSizeController thisMorphSizeController = GetComponent<MorphSizeController>();
    4.         if(thisMorphSizeController.ID == targetID)
    5.         {
    6.             GameObject latestGameObject;
    7.             latestGameObject = this.gameObject;
    8.             thisMorphClone = morphSpawnerScript.morphClones[this.targetID + 1];
    9.  
    10.  
    11.             bool keyReleased = false;
    12.             yield return StartCoroutine(LerpTransformWithKeyCheck(0, 1, attackTime, latestGameObject.transform, keyPressed, () => keyReleased = true));
    13.             Debug.Log("Attack Lerp Finished " + keyPressed);
    14.  
    15.             keyReleased = false;
    16.             yield return StartCoroutine(LerpTransformWithKeyCheck(1, sustainValue, decayTime, latestGameObject.transform, keyPressed, () => keyReleased = true));
    17.             Debug.Log("Decay Lerp Finished " + keyPressed);
    18.  
    19.             if (!keyReleased)
    20.             {
    21.                 // Wait for the same key to be released to continue from the second Lerp
    22.                 while (!Input.GetKeyUp(keyPressed))
    23.                 {
    24.                     yield return null;
    25.                 }
    26.             }
    27.  
    28.             yield return StartCoroutine(LerpTransform(sustainValue, 0, durationRelease, latestGameObject.transform));
    29.             Debug.Log("Release Lerp Finished " + keyPressed);
    30.  
    31.             //Destroy(thisMorphClone);
    32.             latestGameObject.SetActive(false);
    33.         }
    34.        
    35.  
    36.  
    37.     }
    38.  
    39.     private IEnumerator LerpTransform(float start, float end, float duration, Transform lerpingObject)
    40.     {
    41.         //morphSpawnerFinder = GameObject.Find("MorphSpawner");
    42.         //MorphSpawner morphSpawner = morphSpawnerFinder.GetComponent<MorphSpawner>();
    43.  
    44.         float startTime = Time.time;
    45.         float elapsedTime = 0f;
    46.  
    47.         while (elapsedTime < duration)
    48.         {
    49.             elapsedTime = Time.time - startTime;
    50.             float t = elapsedTime / duration;
    51.             lerpingObject.transform.localScale = Vector3.Lerp(Vector3.one * start, Vector3.one * end, t);
    52.  
    53.             yield return null;
    54.         }
    55.     }
    56.  
    57.     private IEnumerator LerpTransformWithKeyCheck(float start, float end, float duration, Transform lerpingObject, KeyCode key, System.Action onKeyReleased)
    58.     {
    59.         //morphSpawnerFinder = GameObject.Find("MorphSpawner");
    60.         //MorphSpawner morphSpawner = morphSpawnerFinder.GetComponent<MorphSpawner>();
    61.  
    62.         float startTime = Time.time;
    63.         float elapsedTime = 0f;
    64.  
    65.         bool sameKeyReleased = false;
    66.  
    67.         while (elapsedTime < duration)
    68.         {
    69.             elapsedTime = Time.time - startTime;
    70.             float t = elapsedTime / duration;
    71.  
    72.             if(targetID == morphSizeController.ID)
    73.                 lerpingObject.transform.localScale = Vector3.Lerp(Vector3.one * start * 100, Vector3.one * end * 100, t);
    74.  
    75.             if (Input.GetKeyUp(key))
    76.             {
    77.                 sameKeyReleased = true;
    78.                 break;
    79.             }
    80.  
    81.             yield return null;
    82.         }
    83.  
    84.         if (sameKeyReleased)
    85.         {
    86.             onKeyReleased?.Invoke();
    87.         }
    88.     }