0
\$\begingroup\$

I increase the scale of the objects when it passes a certain time or in this case, my "difficulty"

    //Spawn Size
float spawnSize = 8;

void Update()
{
    if (Time.time > nextSpawnTime)
    {
        float SpawningInBetween = Mathf.Lerp(spawnsMinMax.y, spawnsMinMax.x, Difficulty.GetDifficultyPercent());
        print(SpawningInBetween);
        nextSpawnTime = Time.time + SpawningInBetween;

        if(Difficulty.GetDifficultyPercent() > 0.7f)
        {
            fallingRockPrefab.transform.localScale = Vector2.one * spawnSize;
        }

Then as expected, the object increases the size after it passed that conditions so i want to revert back to its previous state so i remove:

  if(Difficulty.GetDifficultyPercent() > 0.7f)
    {
        fallingRockPrefab.transform.localScale = Vector2.one * spawnSize;
    }

but when I saved it and go back to unity, the object still in its "increased" size right at the start. What's happening?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Most likely, you are scaling your prefab, which will persist after the program is stopped. Assuming you have some prefab that you have assigned in the inspector, you will need to instantiate it like this:

public MyClass : MonoBehaviour{
   public GameObject rockPrefab; //assign this in the inspector

   void Update(){
    if (Time.time > nextSpawnTime){
      //spawn new rock
      GameObject go = Instantiate(rockPrefab);
      go.transform.position = (your position vector3);
      go.transform.localScale = (your scale vector3);
    }
}

Make sure you later destroy your rocks as they will not "despawn" automatically. Either track them in some sort of List and manually check when to destroy them, or better, attach a script to the rock prefab, add a method that checks for some circumstance (position is greater/less than some value, the amount of time lived is larger than some value, etc) and then call

Destroy(this.gameobject)

Hope this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .