Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 oStaiko · Aug 24, 2016 at 10:26 AM · c#instantiatevariable

Instantiated object affecting variables on the object that instantiated it

I have a GameObject "gun" with a script on it instantiate a projectile with a 'health', however it appears to be treating health as a global variable and causing the gun to shoot bullets that all share the same health pool. The initial health is stored on the gun itself, so that all bullets shot from the gun have this as their initial health. Instead, the health on the gun goes down as well.

The health is supposed to act as a piercing effect, allowing a bullet to go through a certain number of colliders before destroying itself.

Gun Script

 [System.Serializable]  
 public class Weapon 
 {  
     public int damage;   
     public int health;  
     //etc, more public variables here  
 }  

 void public class WepSys  
 {  
     public Weapon wep;  
     void Shoot ()
     {
         GameObject shot = (GameObject) Instantiate(object, position, rotation);
         shot.GetComponent<ProjectileMover> ().wep = wep;
         shot.GetComponent<ProjectileMover> ().iniVel = GetComponent<Rigidbody>().velocity;
     }
 }

ProjectileMover Script

 public class ProjectileMover : MonoBehaviour  
 { 
     public Weapon wep;  
     void OnTriggerEnter( Collider other )  
     {  
         if (wep.health > 0)  
         {  
             wep.health--;  
         }  
         else
         {  
             Destroy (this);
         }
     }
 }
Comment
Add comment · Show 1
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 oStaiko · Aug 24, 2016 at 01:56 AM 0
Share

I just made a new private int on each bullet, copied the health to there, and used that one ins$$anonymous$$d. It works fine, but I'd still like to know why this method doesn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 24, 2016 at 10:42 AM

Weapon is a class. Classes are reference types. In this line:

 shot.GetComponent<ProjectileMover> ().wep = wep;

you assign the same Weapon instance to the newly instantiated object. So both classes ("WepSys" as well as your "ProjectileMover ") will reference the same Weapon instance. If you change something inside the Weapon instance, that change will be "seen" by every class that references the same object.

You might want to create a copy of your Weapon instance. For this you might want to add a "copy constructor". When you add a copy constructor you also need to add a normal one (parameterless):

 [System.Serializable]  
 public class Weapon 
 {  
     public int damage;
     public int health;
     public Weapon() // parameterless constructor
     {
     }
     public Weapon(Weapon aSource) // copy constructor
     {
         damage = aSource.damage;
         health = aSource.health;
         // [ ... ]
     }
     // [ ... ]
 }  

Now instead of assigning the same instance, you will assign a copy of the instance:

 shot.GetComponent<ProjectileMover> ().wep = new Weapon(wep);
Comment
Add comment · Show 1 · 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 oStaiko · Aug 24, 2016 at 03:30 PM 0
Share

Considering Weapon holds about 20 variables it wont be a very pretty code... Oh well I guess, whatever works for now!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Clone of prefabs copy original´s script state during runtime? 0 Answers

How to access value within instantiated class in different object? 1 Answer

How to call a function on an instance 1 Answer

Loading a Constant Array Of Integers 0 Answers


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