Heyo I’m trying to remake crossy roads as a project for learning
The issue is in the SpawnVehicle method
Please let me know if there’s anything else for me to improve
RoadScript.cs
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;
public class RoadScript : MonoBehaviour
{
public GameObject Player;
public Transform Road;
public List<GameObject> VehicleObjects = new List<GameObject>();
public List<Material> Colors = new List<Material>();
public List<Transform> SpawnPoints = new List<Transform>();
public List<GameObject> ActiveVehicles = new List<GameObject>();
public float CarSpeed = 2f;
private Transform Spawn;
private Transform End;
private float sinTime;
private bool CR_running = false;
private void SpawnVehicle()
{
var Colorindex = Random.Range(0, Colors.Count);
var VehicleIndex = Random.Range(0, VehicleObjects.Count);
var Vehicle = Instantiate(VehicleObjects[VehicleIndex], Spawn.position, Quaternion.identity);
Vehicle.transform.SetParent(Road, true);
Vehicle.GetComponent<Renderer>().material = Colors[Colorindex];
ActiveVehicles.Add(Vehicle);
Debug.Log(Vehicle);
}
public float evaluare(float x)
{
return 0.5f * Mathf.Sin(x - Mathf.PI / 2f) + 0.5f;
}
IEnumerator CarSpawn()
{
// CR_running = true;
Debug.Log("Running");
for (int i = 0; i < ActiveVehicles.Count; i++)
{
//int rnumber = Random.Range(0, 200);
// Debug.Log(i);
if (ActiveVehicles[i].transform.position != End.position)
{
sinTime += Time.deltaTime * CarSpeed;
sinTime = Mathf.Clamp(sinTime, 0, Mathf.PI);
float t = evaluare(sinTime);
ActiveVehicles[i].transform.position = Vector3.Lerp(Spawn.position, End.position, t);
}
else
{
ActiveVehicles[i].GetComponent<Renderer>().enabled = false;
ActiveVehicles[i].GetComponent<Rigidbody>().detectCollisions = false;
int waittime = Random.Range(1, 3);
yield return new WaitForSeconds(waittime);
SpawnVehicle();
}
//if (rnumber == 4)
// {
// int waittime = Random.Range(1, 3);
// yield return new WaitForSeconds(waittime);
// SpawnVehicle();
// }
}
CR_running = false;
}
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
var index = Random.Range(0, SpawnPoints.Count);
Spawn = SpawnPoints[index];
SpawnPoints.RemoveAt(index);
End = SpawnPoints[0];
SpawnVehicle();
// StartCoroutine(CarSpawn());
// Debug.Log("Load????");
}
// Update is called once per frame
void Update()
{
// Debug.Log(Player.transform.position.z - Road.position.z);
if ( Player.transform.position.z - Road.position.z > 5 && CR_running == false)
{
StartCoroutine(CarSpawn());
}
else
{
for (int i = 0; i < ActiveVehicles.Count; i++)
{
DestroyImmediate(ActiveVehicles[i]);
}
}
}
}
Hey, It’s quiet difficult to debug your code without understanding/seeing what errors you’re receiving.
Could you add the exception, along with the line that the exception is occuring on.
Also, have you tried debugging to see the order of execution as typically this is what’ll cause you issues?
Thanks for everyone helping, I’ve found the issue in the Update method. I had a bit of code to stop lag and it didn’t have adequate if statements.
Before
void Update()
{
// Debug.Log(Player.transform.position.z - Road.position.z);
if ( Player.transform.position.z - Road.position.z > 5 && CR_running == false)
{
StartCoroutine(CarSpawn());
}
else
{
for (int i = 0; i < ActiveVehicles.Count; i++)
{
DestroyImmediate(ActiveVehicles[i]);
}
}
}
After
void Update()
{
// Debug.Log(Player.transform.position.z - Road.position.z);
if ( Player.transform.position.z - Road.position.z > 5 && CR_running == false)
{
StartCoroutine(CarSpawn());
}
else
{
for (int i = 0; i < ActiveVehicles.Count; i++)
{
if (ActiveVehicles[i].transform.position.x == End.position.x && ActiveVehicles[i].GetComponent<Renderer>().enabled == false)
{
DestroyImmediate(ActiveVehicles[i]);
}
}
}
}