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;
}
}
}
.