У меня есть префабы Stone и Potion, которые сами по себе отлично работают. Но стоит только запустить их через Spawner, сразу начинается что-то необъяснимое. Префаб Stone продолжает работать, а вот Potion теряет все свойства из скрипта.
Помогите разобраться, пожалуйста.
Вот скрипт Stone.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stone : MonoBehaviour
{
GameObject HP2;
hp2 hpScript2;
GameObject HP1;
hp1 hpScript1;
GameObject Player1;
HeroKnight P1;
GameObject Player2;
HeroKnight2 P2;
void Start()
{
HP2 = GameObject.Find("2hp");
hpScript2 = HP2.GetComponent<hp2>();
HP1 = GameObject.Find("1hp");
hpScript1 = HP1.GetComponent<hp1>();
Player1 = GameObject.Find("HeroKnight1");
P1 = Player1.GetComponent<HeroKnight>();
Player2 = GameObject.Find("HeroKnight2");
P2 = Player2.GetComponent<HeroKnight2>();
}
// Update is called once per frame
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("P1"))
{
P1.Hurt();
hpScript1.Reduce10Hp1();
Destroy(gameObject);
}
if (other.CompareTag("P2"))
{
P2.Hurt();
hpScript2.Reduce10Hp2();
Destroy(gameObject);
}
if (other.CompareTag("Ground"))
{
Destroy(gameObject);
}
}
}
Potion.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlusHP : MonoBehaviour
{
GameObject HP2;
hp2 hpScript2;
GameObject HP1;
hp1 hpScript1;
GameObject posion;
void Start()
{
HP2 = GameObject.Find("2hp");
hpScript2 = HP2.GetComponent<hp2>();
HP1 = GameObject.Find("1hp");
hpScript1 = HP1.GetComponent<hp1>();
posion = GameObject.Find("Potion");
}
private void Update()
{
Destroy(posion, 3f);
}
// Update is called once per frame
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("P1"))
{
hpScript1.Add10Hp1();
Destroy(posion);
}
if (other.CompareTag("P2"))
{
hpScript2.Add10Hp2();
Destroy(posion);
}
}
}
Spawner.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] Prefabs;
float timer;
public float interval;
void Update()
{
int y = Random.Range(0, 3);
timer += Time.deltaTime;
if (timer > interval)
{
timer = 0;
Instantiate(Prefabs[y], transform.position, transform.rotation);
}
}
}