N
N
nikki17072021-01-29 00:24:19
Unity
nikki1707, 2021-01-29 00:24:19

Why is the prefab script disabled after cloning?

I have Stone and Potion prefabs which work great on their own. But as soon as you launch them through Spawner, something inexplicable immediately begins. The Stone prefab continues to work, but Potion loses all properties from the script.
Help to understand, please.

Here is the Stone script.
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);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question