- Home /
Question by
StuwuStudio · Mar 18, 2015 at 05:26 AM ·
instantiatedestroydistanceloopcalculate
Generate randoms planets with a specific script
Hi everybody! I have a litte problem for adapt a script to a multiple planets script, my first script is for calculate distance between two object and destroy the object if the object is too far of the player and instantiate if the player come back.
using UnityEngine;
using System.Collections;
public class Calculate : MonoBehaviour {
float distance;
public Transform object1;
public Transform object2;
public Transform ozone;
Transform planets;
public Texture2D texture1 = null;
public Texture2D texture2 = null;
public Texture2D texture3 = null;
void OnGUI () {
string distanceText = System.Convert.ToString(distance);
GUI.Label (new Rect(Screen.width/56, Screen.height/3, 200, 200), distanceText);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Calculate Distance
distance = Vector3.Distance (object1.transform.position, object2.transform.position);
//Detecte distance
if(distance <= 200) {
object2.transform.renderer.material.mainTexture = texture1;
}
//Detecte distance
if(distance >= 400) {
object2.transform.renderer.material.mainTexture = texture2;
}
//Detecte distance
if(distance >= 600) {
object2.transform.renderer.material.mainTexture = texture3;
}
//Detecte distance
if(distance <= 100) {
object2.transform.renderer.enabled = false;
object2.transform.collider.enabled = false;
if(GameObject.Find("Planets01(Clone)") == false && GameObject.Find("Planets01") == false) {
Instantiate(Resources.Load("Planets01"), ozone.transform.position, Quaternion.identity);
}
if(GameObject.Find("Planets01") == false && GameObject.Find("Planets01(Clone)") == false) {
Instantiate(Resources.Load("Planets01"), ozone.transform.position, Quaternion.identity);
}
}
//Detecte distance
if(distance >= 100) {
object2.transform.renderer.enabled = true;
object2.transform.collider.enabled = true;
Destroy(GameObject.Find("Planets01"));
Destroy(GameObject.Find("Planets01(Clone)"));
}
}
}
the second is an random planets distributor (look down) but what i want to do is when i approche a planets, there replace the "false planet (ozone)" by the real "planets01" and the inverse when i go away. (this script don't really work)
using UnityEngine;
using System.Collections;
public class PlanetsRandomGenerating : MonoBehaviour {
public GameObject planet01;
public GameObject Ozone;
public float planetsNumber;
GameObject newPlanets;
string planetsName;
float floatName;
float near;
float distance;
Transform Allpl = GameObject.FindGameObjectsWithTag("Planets01");
Transform Player = GameObject.FindGameObjectWithTag("Player");
public Material skybox1 = null;
public Material skybox2 = null;
// Use this for initialization
void Start () {
for(int i = 1; i <= planetsNumber; i++) {
floatName = floatName + 1f;
Vector3 position01 = new Vector3(Random.Range(-10000.0f, 10000.0f), Random.Range(-10000.0f, 10000.0f), Random.Range(-10000.0f, 10000.0f));
newPlanets = (GameObject)Instantiate(planet01, position01, Quaternion.identity); //Assing the skybox script to the current planets create a cuurent pla for each planets
planetsName = System.Convert.ToString(floatName);
newPlanets.name = planetsName;
Instantiate(Ozone, position01, Quaternion.identity);
}
}
// Update is called once per frame
void Update () {
near = Vector3.Distance (Player.transform.position, Allpl.transform.position);
Debug.Log(near);
//distance = Vector3.Distance (Player.transform.position, Unknow.transform.position);
}
}
help please! thank
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613190445im_/https://answers.unity.com/themes/thub/images/avi.jpg)