Adding different values to randomly created objects
Hi, right now my code spawns random objects from a prefab and assigns random values to them. The problem is, when the character collides with any of them the destroy function takes the value of the last created object. I want to take value of the object that collides with my character. How can I do this.
This is my script to create enemies. (Attached to "Main Camera").
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
public GameObject enemyPrefab;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
void Start()
{
StartCoroutine(enemyWave());
}
void Update()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
private void spawnEnemy()
{
GameObject a = Instantiate(enemyPrefab) as GameObject;
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y);
}
IEnumerator enemyWave()
{
while(true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
This is my script for the enemies. (Attached to "Enemy" Prefab).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
private Vector2 screenBounds;
public TextMeshProUGUI textEnemy;
public static int enemyValue;
private int characterV;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, -speed);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
void Awake()
{
characterV = GameObject.Find("Character").GetComponent<Destroying>().characterValue;
GameObject[] objs;
objs = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject Enemy in objs)
{
enemyValue = Random.Range((characterV / 2) - 2, (characterV * 2) - 2);
textEnemy.text = enemyValue.ToString();
}
}
void Update()
{
if(transform.position.y < -screenBounds.y)
{
Destroy(this.gameObject);
}
}
}
This is my script for collision. (Attached to "Character" GameObject).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Destroying : MonoBehaviour
{
public int characterValue = 10;
[SerializeField] float destroyDelay = 0 ;
private static int enemyV;
public TextMeshProUGUI textChar;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy" )
{
//enemyV = GameObject.FindWithTag("Enemy").GetComponent<Enemy>().enemyValue;
enemyV = Enemy.enemyValue;
if(enemyV > characterValue){
Debug.Log("Öldü");
Debug.Log(enemyV);
Debug.Log(characterValue);
}
Destroy(other.gameObject , destroyDelay );
//characterValue = characterValue + enemyV;
//Debug.Log("Yeni Karakter Değeri = " + characterValue);
}
}
void Update()
{
textChar.text = characterValue.ToString();
}
}
Your answer
Follow this Question
Related Questions
Third person controller moving backwards when shooting 0 Answers
Make a collision check when spawning with Instantiate 1 Answer
Instantine - none collider 0 Answers
Collision Detection between two different Prefabs doesnt work 1 Answer
If Statement not working properly - Comparing game objects 0 Answers