- Home /
Instantiate a COMPLETELY Unique Instance of an Object
Hey, right now I'm instantiating a number of various types of a GameObject. There are a number of different prefabs and the scripts tells the game which prefab to instantiate based on a button press. I'm instantiating them by setting them equal to a temporary value, which I thought made them unique of each other, but when a static variable changes in one instance, it changes in all of them, which I don't want to happen. I tried to Instantiate into an array of GameObjects and increase the element that the game is going to create the next GameObject in, however when I did that, all of the code beneath it seemed to be ignored (allowing the player to spawn hundreds of these prefabs at a time).
So my question is, is there a better way to Instantiate a Prefab of a GameObject into the scene and make it completely unique from all other similar instances of that GameObject? Thanks ahead of time :)
Since everyone wants to see my code, I'll post some of it. The whole script is a good 800 lines, so this is just the snippet I'm directly referring to for the sake of Instantiating:
if (placeAnimal == true && notSpawn == true)
{
notSpawn = false;
}
else if (placeAnimal == true && notSpawn == false)
{
if (Input.GetKeyDown(KeyCode.Mouse0) && placeAnimal == true)
{
Debug.DrawRay (placeRay.origin, placeRay.direction * rayLength, Color.green);
print("Ray Drawn");
placeRay = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (placeRay, placeHit, rayLength))
{
placeHitVector = placeHit.point;
}
if (placeHit.collider.gameObject.tag == cageToCheck)
{
UpdateNewsFeed();
script_ParkManager.newsFeed[0] = "You purchased a " + animalToPlace.gameObject.tag + ".";
animalToPlace.transform.position = script_DetermineClickedObjects.currentHitSpot;
spawnedAnimalInstance = Instantiate(animalToPlace, testHit, Quaternion.identity);
placeAnimal = false;
script_ParkManager.currentMoney -= moneyToRemove;
animalToPlace = null;
targetToggle = false;
}
else
{
UpdateNewsFeed();
script_ParkManager.newsFeed[0] = "Failed to place " + animalToPlace.gameObject.tag + ".";
placeAnimal = false;
animalToPlace = null;
targetToggle = false;
}
}
}
Now as for the GetComponent part of the script, in this instance I have an animal who can get sick over time based on a couple variables. isSick is a static var so that a Veterinarian can see the sick animal and heal it.
function UpdateHealth () {
sickRandom = Random.Range(0,101);
if (sickRandom <= sickMinRange && isSick == false)
{
GameObject.FindGameObjectWithTag("MainObject").GetComponent(script_HUDBuild).UpdateNewsFeed();
if (!(animalName == ""))
{
script_ParkManager.newsFeed[0] = "" + animalName + " the " + this.gameObject.tag + " is sick! Make sure you have a Medical Aid to cure them.";
}
else
{
script_ParkManager.newsFeed[0] = "A " + this.gameObject.tag + " is sick! Make sure you have a Medical Aid to cure them.";
}
sickObject = Instantiate(sickAnimation, transform.position, transform.rotation);
isSick = true;
InvokeRepeating("Sick", 1.0, 2.0);
}
}
Now here is the script for the Veterinarian. He collides with the animal in order to heal him.
#pragma strict
var medicalSkill : int;
function Start () {
}
function Update () {
}
function OnTriggerEnter ( other : Collider) {
if (other.gameObject.tag == "Zebra" || other.gameObject.tag == "Elephant" || other.gameObject.tag == "Tiger" || other.gameObject.tag == "Bear" || other.gameObject.tag == "Giraffe" || other.gameObject.tag == "Monkey")
{
if (other.gameObject.GetComponent(script_Animal).isSick == false)
{
GameObject.FindGameObjectWithTag("MainObject").GetComponent(script_HUDBuild).UpdateNewsFeed();
script_ParkManager.newsFeed[0] = "A Medical Aid has cured a " + other.gameObject.tag + ". Congratulations!";
}
other.gameObject.GetComponent(script_Animal).animalHealth += Random.Range(10,51);
other.gameObject.GetComponent(script_Animal).isSick = false;
other.gameObject.GetComponent(script_Animal).sickMinRange -= Random.Range(20, 51);
}
}
Am I wrong to use a static var here then? Should I be setting the GetComponent(script_Animal) equal to a
var scriptInstance : script_Animal;
or something along those lines?
I'm not sure why you are getting this behavior.
The only reason your object will share the same var, is if you point them to the same var.
If each of them has its own script with its own vars, it shouldn't happen.
Can we see your code? We cannot fix a problem we cannot see! :)
Answer by Eric5h5 · Mar 11, 2013 at 09:41 PM
when a static variable changes in one instance, it changes in all of them
Yes, that's the entire point of static variables. Don't use static unless you specifically want all the instances to be the same.
How would you go about accessing them then? I have used GEtComponent(scriptname) to access the variables, but it tells me that it still cannot access them without a static version. Would I create a temporary variable for that script and then refer to that variable? If so, how would I declare that? Would it be of type :scriptname?
You don't show code or specific error messages. Please provide those. GetComponent IS the way to do it, so you're doing something wrong...
How would you go about accessing them then?
Just use GetComponent as described in the docs, under "Accessing other GameObjects". Variables must be public to be accessed by other scripts.