Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BenKurdziel · Mar 11, 2013 at 08:44 PM · instantiateobjectinstanceunique

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dorpeleg · Mar 11, 2013 at 09:30 PM 0
Share

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.

avatar image programmrzinc · Mar 11, 2013 at 09:34 PM 1
Share

Can we see your code? We cannot fix a problem we cannot see! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BenKurdziel · Mar 11, 2013 at 09:59 PM 0
Share

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?

avatar image DaveA · Mar 11, 2013 at 10:03 PM 0
Share

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

avatar image Eric5h5 · Mar 12, 2013 at 12:25 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I restrict game to only instantiate 1 instance of object? 1 Answer

NullReference Exception when adding a class 2 Answers

Accessing variable in a specific instance of a prefab 1 Answer

How to instantiate object onto other object from script 1 Answer

Lock rotation of an instantiated object to another 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges