简体   繁体   English

Unity实例化对象脚本不重复吗?

[英]Unity Instantiated Object Script not repeating?

Ok I have well... 2 Questions 好吧,我有... 2个问题

my first Question: If I instantiate an object, do the script stay on it, and the script starts like how they were in the beginning of the game. 我的第一个问题:如果我实例化一个对象,脚本是否停留在该对象上,该脚本的开始就像它们在游戏开始时一样。

my second Question: I instantiated my object, but I keep getting this error 我的第二个问题:我实例化了我的对象,但我不断收到此错误

UnassignedReferenceException: The variable target of GreenGoUp has not been assigned. UnassignedReferenceException:GreenGoUp的变量目标尚未分配。 You probably need to assign the target variable of the GreenGoUp script in the inspector. 您可能需要在检查器中分配GreenGoUp脚本的目标变量。 GreenGoUp.Update () (at Assets/Resources/Scripts/GreenGoUp.cs:23) GreenGoUp.Update()(在Assets / Resources / Scripts / GreenGoUp.cs:23)

well basically lets say I have Envelopes(which are objects), I thorw my object(which is a prefab) into a trigger, and when it hits the trigger, the object is destroyed... and then I instantiate the object back to its starting position, But if I try to throw my object onto the trigger again. 好吧,基本上可以说我有信封(是对象),我将我的对象(是预制件)解扣到触发器中,当它碰到触发器时,对象被破坏了……然后我将对象实例化回它的起始位置,但是如果我尝试再次将对象扔到触发器上。 nothing happens. 什么都没发生。

The thing is I have a script in the maincamera that does a random range and puts it into a variable and depending on the number, a script runs. 关键是我在主摄像机中有一个脚本,该脚本执行随机范围并将其放入变量中,并根据数字运行脚本。 ill display my script now for one of the Envelopes. 现在无法显示其中一个信封的脚本。

MainCamera Script MainCamera脚本

using UnityEngine;
using System.Collections;

public class RandomFunction : MonoBehaviour {

    int n;
    public GameObject blueObject= null;
    public GameObject greenObject= null;
    public GameObject yellowObject= null;
    public GameObject redObject= null;
    public GameObject orangeObject= null;
    public GameObject purpleObject= null;

    void Start () 
    {
        n=Random.Range(0,1);

        switch(n)
        {
        case 0:

            greenObject.GetComponent<GreenGoUp> ().enabled = true;
            Debug.Log ("GreenGoUp Should be Working");
            break;
        }
    }
}

Script for one of the envelopes 信封之一的脚本

using UnityEngine;
using System.Collections;

public class GreenEnvelope : MonoBehaviour
{
    bool isMove = false;
    public float speed = 40;
    Vector3 targetPosition;
    Vector3 currentPosition;
    Vector3 directionOfTravel ;
    public Transform target;
    public GameObject playerObject;
    GameObject Green=null;
    //public GameObject playerObject;


    void Start()
    {
        playerObject.GetComponent<OnTrig>().enabled=true;
        Debug.Log ("GreenEnvelope is now on, I should be moving");
    }

    void Update()
    {
        Debug.Log ("GreenEnvelope is now moving");
        targetPosition = target.transform.position; // Get position of object B
        currentPosition = this.transform.position; // Get position of object A
        directionOfTravel = targetPosition - currentPosition;
        if (Vector3.Distance (currentPosition, targetPosition) > .2f) {
            this.transform.Translate (
                (directionOfTravel.x * speed * Time.deltaTime),
                (directionOfTravel.y * speed * Time.deltaTime),
                (directionOfTravel.z * speed * Time.deltaTime),
                Space.World);
            transform.Rotate (new Vector3 (Time.deltaTime * 400, 0, 0));
        } 
        }
}

This script lift the envelope a little so It can be displayed to the user so he can throw it 该脚本将信封抬起一点,以便可以显示给用户,以便他可以扔它

using UnityEngine;
using System.Collections;

public class GreenGoUp : MonoBehaviour {

    Vector3 targetPosition;
    Vector3 currentPosition;
    Vector3 directionOfTravel;
    public Transform target;

    //public GameObject playerObject;
    public float speed = 40;



    void Start () {
        Debug.Log ("GreenGoUp IS WORKING");
    }


    void Update () {
        Debug.Log ("GreenGoUp IS GOING UP");
        targetPosition = target.transform.position; // Get position of object B
        currentPosition = this.transform.position; // Get position of object A
        directionOfTravel = targetPosition - currentPosition;
        if (Vector3.Distance (currentPosition, targetPosition) > .1f) {
            this.transform.Translate (
                (directionOfTravel.x * speed * Time.deltaTime),
                (directionOfTravel.y * speed * Time.deltaTime),
                (directionOfTravel.z * speed * Time.deltaTime),
                Space.World);
        } 
    }
}

This script is on the Trigger that destroys the object 该脚本位于销毁对象的触发器上

using UnityEngine;
using System.Collections;

public class OnTrig : MonoBehaviour {

    ParticleSystem particle;
    public GameObject playerObject;
    public GameObject greenDestroy;
    bool isMove=false;
    public GameObject Purple;
    public GameObject Green;
    public GameObject Blue;
    public GameObject Red;
    public GameObject Orange;
    public GameObject Yellow;
    public GameObject mainCamera;



    void OnTriggerEnter(Collider col)
    {
         if(col.gameObject.tag == "Green")
        {
            Destroy(col.gameObject);
            Debug.Log ("GreenEnvelope Has Been Destroyed");
            Green = Instantiate(Resources.Load("Prefabs/greenEnvelope"),new Vector3(-11.63f,-10.49f,30.09f), Quaternion.identity) as GameObject;
            mainCamera.GetComponent<RandomFunction>().enabled=true;

        }
    }
    void OnMouseDown()
    {
        if (Input.GetMouseButtonDown (0)) {
            isMove = true;
        }

        if (isMove == true) {
            playerObject.GetComponent<GreenEnvelope> ().enabled = true;
        }
    }
}

I cannot answer question #1. 我无法回答问题1。 But for question #2 to fix the error you have to assign the object for GreenGoUp. 但是对于要解决错误的问题2,您必须为GreenGoUp分配对象。 Like how this person hasn't assigned Sight in this image: 就像这个人没有在这张图片中分配Sight一样: 图片 .

Answer 1: 答案1:

Yes, after instantiation of a prefab, all their scripts and their values will be copied to the instance. 是的,在实例化预制之后,它们的所有脚本及其值都将复制到实例中。 If however you are instantiating a runtime object (for example an object you instantiated earlier) you may run into surprises, because the new instance will copy all the values from your already initialized object, and then try to initialize it, which will likely produce inconsistent state. 但是,如果要实例化运行时对象(例如,先前实例化的对象),则可能会遇到意外,因为新实例将复制您已初始化的对象中的所有值,然后尝试对其进行初始化,这可能会导致不一致州。

Answer 2: 答案2:

I assume you have Green, Blue etc. objects as instances of prefabs in your scene. 我假设您有Green,Blue等对象作为场景中的预制实例。 For those instances, you set the "target" field of your Green(/Blue/Red)GoUp scripts to another scene object. 对于这些实例,可以将Green(/ Blue / Red)GoUp脚本的“目标”字段设置为另一个场景对象。

So far so good. 到现在为止还挺好。 Game will run and work until your Green scene object is destroyed and reinstantiated. 游戏将运行并运行,直到您的绿色场景对象被销毁并重新实例化。 Instantiate() will create a new instance of your Green prefab, without "target" being set, because Prefabs can not hold references to scene objects. Instantiate()将创建绿色Prefab的新实例,而无需设置“目标”,因为Prefabs无法保存对场景对象的引用。

You'll have to reassign "target" after instantiating your object. 实例化对象后,您必须重新分配“目标”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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