Question by
matthewyanglele · Aug 03, 2020 at 11:25 AM ·
c#scripting problemno error
Clones of Object Not Moving
I'm new to Unity, and I'm making a simple game where the player has to dodge randomly spawning and moving obstacles. However, when a clone of the obstacle spawns in, it doesn't move. The actual coordinates of the obstacle aren't changing and I think it might be because of the code that uses variables from other scripts. This is the code of ObstacleMovement.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleMovement : MonoBehaviour
{
private ObstacleCloning instantiatedObject;
private ObstacleCloning1 instantiatedObject1;
private ObstacleCloning2 instantiatedObject2;
private ObstacleCloningThree instantiatedObject3;
private ObstacleCloning4 instantiatedObject4;
public float speed = 5f;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
instantiatedObject = ObstacleCloning.FindObjectOfType<ObstacleCloning>();
instantiatedObject1 = ObstacleCloning.FindObjectOfType<ObstacleCloning1>();
instantiatedObject2 = ObstacleCloning.FindObjectOfType<ObstacleCloning2>();
instantiatedObject3 = ObstacleCloning.FindObjectOfType<ObstacleCloningThree>();
instantiatedObject4 = ObstacleCloning.FindObjectOfType<ObstacleCloning4>();
instantiatedObject.transform.position += Vector3.forward*speed*Time.deltaTime;
instantiatedObject1.transform.position += Vector3.forward*speed*Time.deltaTime;
instantiatedObject2.transform.position += Vector3.forward*speed*Time.deltaTime;
instantiatedObject3.transform.position += Vector3.forward*speed*Time.deltaTime;
instantiatedObject4.transform.position += Vector3.forward*speed*Time.deltaTime;
}
}
and this is the code of ObstacleCloning, ObstacleCloning1, ObstacleCloning2, ObstacleCloningThree, and ObstacleCloning4:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCloning : MonoBehaviour
{
private float time = 0.0f;
public float interpolationPeriod = 2.3f;
public GameObject obstaclePrefab;
public Transform[] spawnPoints;
public GameObject instantiatedObj;
// Start is called before the first frame update
void Start(){
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
if(time>=interpolationPeriod){
time = 0.0f;
instantiatedObj = (GameObject)Instantiate(obstaclePrefab, spawnPoints[Random.Range(0,20)].position,Quaternion.identity);
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.name=="Despawn"){
Destroy(instantiatedObj);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCloning1 : MonoBehaviour
{
private float time = 0.0f;
public float interpolationPeriod = 2.3f;
public GameObject obstaclePrefab;
public Transform[] spawnPoints;
public GameObject instantiatedObj1;
// Start is called before the first frame update
void Start(){
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
if(time>=interpolationPeriod){
time = 0.0f;
instantiatedObj1 = (GameObject)Instantiate(obstaclePrefab, spawnPoints[Random.Range(0,20)].position,Quaternion.identity);
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.name=="Despawn"){
Destroy(instantiatedObj1);
}
}
}
_________________________________________________________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCloning2 : MonoBehaviour
{
private float time = 0.0f;
public float interpolationPeriod = 2.3f;
public GameObject obstaclePrefab;
public Transform[] spawnPoints;
public GameObject instantiatedObj2;
// Start is called before the first frame update
void Start(){
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
if(time>=interpolationPeriod){
time = 0.0f;
instantiatedObj2 = (GameObject)Instantiate(obstaclePrefab, spawnPoints[Random.Range(0,20)].position,Quaternion.identity);
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.name=="Despawn"){
Destroy(instantiatedObj2);
}
}
}
_________________________________________________________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCloningThree : MonoBehaviour
{
private float time = 0.0f;
public float interpolationPeriod = 2.3f;
public GameObject obstaclePrefab;
public Transform[] spawnPoints;
public GameObject instantiatedObj3;
// Start is called before the first frame update
void Start(){
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
if(time>=interpolationPeriod){
time = 0.0f;
instantiatedObj3 = (GameObject)Instantiate(obstaclePrefab, spawnPoints[Random.Range(0,20)].position,Quaternion.identity);
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.name=="Despawn"){
Destroy(instantiatedObj3);
}
}
}
_________________________________________________________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCloning4 : MonoBehaviour
{
private float time = 0.0f;
public float interpolationPeriod = 2.3f;
public GameObject obstaclePrefab;
public Transform[] spawnPoints;
public GameObject instantiatedObj4;
// Start is called before the first frame update
void Start(){
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
if(time>=interpolationPeriod){
time = 0.0f;
instantiatedObj4 = (GameObject)Instantiate(obstaclePrefab, spawnPoints[Random.Range(0,20)].position,Quaternion.identity);
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.name=="Despawn"){
Destroy(instantiatedObj4);
}
}
}
Any help will be really helpful!
Comment