- Home /
Clones with individual scripts
I'm still having difficulty with instantiated prefabs. I've made a small star map to see if I can change a variable on an individual prefab but I seem to have hit a brick wall. The following scripts show no errors but the debug.log fails to print anything. The first script is the clone manager which is attached to an empty game object in the hierarchy, the second is attached to the prefab.
StarGenScript:
var BasicStarPrefab:GameObject; // The prefab I am cloning var CloneScript:BasicStarScript; // The script that should be attached to each clone var StarGenScript:StarGenScript; // The clone managers script
//The basic attributes of each star static var XCurrentStar; static var YCurrentStar; static var ZCurrentStar; static var CurrentStarType; static var CurrentStarNumber;
// Call this function once at the start function Awake () { StarGenerate(); }
function StarGenerate (){ for (var CountNum : int = 0;CountNum < 1000; CountNum++) { // Randomly determine the position of each star XCurrentStar = Random.Range(0.0, 10.0); YCurrentStar = Random.Range(0.0, 10.0); ZCurrentStar = Random.Range(0.0, 10.0);
// Set the identification number and type of star
CurrentStarNumber = CountNum;
CurrentStarType = Random.Range(1, 2);
// Make a vector of the random coordiantes and instantiate a star
var position = Vector3(XCurrentStar,YCurrentStar,ZCurrentStar);
var NewBasicStarPrefab = Instantiate(BasicStarPrefab, position, Quaternion.identity);
NewBasicStarPrefab.name = "BasicStar"; // Rename the clone to BasicStar
// Access the clones script and send some variables to it
CloneScript = NewBasicStarPrefab.GetComponent(BasicStarScript);
CloneScript.StarNumber = StarGenScript.CurrentStarNumber;
CloneScript.StarType = StarGenScript.CurrentStarType;
}
}
BasicStarScript:
static var StarType; static var StarNumber;
function Update () { Debug.Log(StarNumber); Debug.Log(StarType);
if(StarType==1){
renderer.material.color = Color.black;
}
if(StarType==2){
renderer.material.color = Color.white;
}
}
If someone could fix these scripts and post a working pair I would be eternally grateful
Answer by lhk · Oct 26, 2010 at 03:44 PM
Static variables are shared by all scripts of one type. As you have attached the BasicStartScript to the prefab each object has an "individual" copy of this script but there are no "individual" variables in it. You could try to send a message to your new star object to set the values. Try these scripts (in c#) StarGenScript
using UnityEngine; using System.Collections;
public class StarGen : MonoBehaviour { float XCurrentStar; float YCurrentStar; float ZCurrentStar; float CurrentStarType; float CurrentStarNumber; Transform BasicStarPrefab;
// Use this for initialization void Start () { StarGenerate(); }
// Update is called once per frame void Update () {
}
void StarGenerate() { for (int CountNum = 0;CountNum < 1000; CountNum++) { // Randomly determine the position of each star XCurrentStar = Random.Range(0.0f, 10.0f); YCurrentStar = Random.Range(0.0f, 10.0f); ZCurrentStar = Random.Range(0.0f, 10.0f);
// Set the identification number and type of star
CurrentStarNumber = CountNum;
CurrentStarType = Random.Range(1, 2);
// Make a vector of the random coordiantes and instantiate a star
Vector3 position =new Vector3(XCurrentStar,YCurrentStar,ZCurrentStar);
GameObject NewBasicStarPrefab =(GameObject) Instantiate(BasicStarPrefab, position, Quaternion.identity);
NewBasicStarPrefab.name = "BasicStar"; // Rename the clone to BasicStar
// Access the clones script and send some variables to it
NewBasicStarPrefab.SendMessage("SetStarNumber",CurrentStarNumber);
NewBasicStarPrefab.SendMessage("SetStarType",CurrentStarType);
}
}
}
BasicStarScript
using UnityEngine;
using System.Collections;
public class BasicStarScript : MonoBehaviour { int StarType; int StarNumber;
void Update () {
// I don't think you want to write something to the
// Debug output everytime Update is called
//Debug.Log(StarNumber);
//Debug.Log(StarType);
if(StarType==1)
{
renderer.material.color = Color.black;
}
if(StarType==2)
{
renderer.material.color = Color.white;
}
}
// Use this for initialization void Start () {
}
// Update is called once per frame void SetStarNumber(int number) { StarNumber=number; } void SetStarType(int type) { StarType=type; }
}
I hope this works and I could help you.