0

Ok I have well... 2 Questions

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. You probably need to assign the target variable of the GreenGoUp script in the inspector. GreenGoUp.Update () (at 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

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;
        }
    }
}
1
  • If any of the answers worked for you, it would be helpful for others if you accept that answer. If not, feel free to comment on the answers given, or update your question. Commented Jan 27, 2016 at 16:32

2 Answers 2

1

I cannot answer question #1. But for question #2 to fix the error you have to assign the object for GreenGoUp. Like how this person hasn't assigned Sight in this image: image.

Sign up to request clarification or add additional context in comments.

4 Comments

Mine does work for the first time, so it is assigned... but it only gives me that error after I instantiate it and try to throw the envelope again.
@user5717696 Try checking if it is still assigned after it gives you the error maybe it is some how becoming unassigned.
actually you are right, right after I do it the first time, I click on my object and it is unassigned...is there a fix for this?
@user5717696 yes in your script set greengoup using one of unitys findobject methods like findobjectbytag but make sure if you use that you have to give greengoup a tag if you don't know what I'm talking about google findobject unity (I'm on my phone right now I don't want to type a lot)
0

Answer 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:

I assume you have Green, Blue etc. objects as instances of prefabs in your scene. For those instances, you set the "target" field of your Green(/Blue/Red)GoUp scripts to another scene object.

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.

You'll have to reassign "target" after instantiating your object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.