- Home /
My prefabs aren't keeping the float value for speed on the mover script,Im having a problem when a new prefab is spawned it doesn't keep the floated value for speed
The mover script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MeteorMover : MonoBehaviour { public float speed = -2f;
Rigidbody2D rigidBody;
// Start is called before the first frame update
IEnumerator Start()
{
rigidBody = GetComponent<Rigidbody2D>();
// Give meteor an initial downward velocity
rigidBody.velocity = new Vector2(0, speed);
WaitForSeconds wait = new WaitForSeconds(1);
int time = 1;
while (time > 0)
{
speed -= .1f;
time++;
//yield return speed;
yield return wait;
}
//yield return speed;
yield return wait;
}
} The spawner script System.Collections; using System.Collections.Generic; using UnityEngine;
public class MeteorSpawn : MonoBehaviour { public GameObject meteorPrefab; public float minSpawnDelay = 1f; public float maxSpawnDelay = 3f; public float spawnXLimit = 6f;
// Start is called before the first frame update
void Start()
{
Spawn();
}
void Spawn()
{
// Create a meteor at a random x position
float random = Random.Range(-spawnXLimit, spawnXLimit);
Vector3 spawnPos = transform.position + new Vector3(random, 0f, 0f);
Instantiate(meteorPrefab, spawnPos, Quaternion.identity);
Invoke("Spawn", Random.Range(minSpawnDelay, maxSpawnDelay));
}
}
Your answer
Follow this Question
Related Questions
Problem with using Vector3.MoveTowards on randomly instantiated target 1 Answer
I cannot get my code to function correctly, and do not know what to do. Please help. 1 Answer
I can modify the GameObject transform using the inspector, how can I modify the mesh as well? 0 Answers
Change script from 3d to 2d Game. 3 Answers