简体   繁体   中英

My code runs more than one time in the whole Unity project

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankItemScript : MonoBehaviour
{   
    public GameObject tankFollowerPrefab;
    public bool a = true;
    public GameObject laptopPrefab;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            GameObject tank = Instantiate(tankFollowerPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1.5f), Quaternion.identity, transform);
            Destroy(gameObject);
        }
    }

    void Update()
    {
        transform.Rotate(0f, 0.2f, 0f);
    }

    void Start()
    {
        if (a == true)
        {
            Instantiate(laptopPrefab, new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z), transform.rotation);
            a = false;
        }
    }
}

void Start() running 3 times. Even with checking that I running it ONE time.

There is no other scripts that are affecting this script. My laptop prefab is used only in this code.

Why instead of one laptop it spawns 3-4? How to fix it? The problem is that it's happening not only with this script, but with others too! Instead of adding 1 to my variable, it adds 3!

Even more, it worked before! I feel that memes become reality D:

您的问题很可能是您将此脚本(使用此启动函数)放在多个对象上,然后每个对象都调用其启动函数。

也许我误解了这个问题,但是您是否尝试过从更新方法中删除此代码?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM