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 Jacob_Anthony · Feb 13, 2017 at 12:10 PM · c#gameobjectvariables

changin functionality of gameobject/script without loosing variables C#

How to change a script/functionality of gameobject without loosing a variables in that script? I want to do something like this: if object has less then 0 hp it becomes our and if so it must have functionality like our objects but i didn't want to just instantiate another prefab but only change functionality. How can I do this? Maybe something with classes? btw. sorry for my english

Comment
Add comment
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

1 Reply

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

Answer by Chikari · Feb 13, 2017 at 01:54 PM

Store your "global" variables in another class and pass the reference to your second script before switching functionality (=enabling the second behaviour, disabling the first behaviour).

 public class A: MonoBehaviour {
  StorageClass storage;
  
  public void doSomething() {
   storage.thisVar = 5;
  }
  
  public void switchFunctionality() {
   GetComponent < B > ().enabled = true;
   GetComponent < B > ().setStorageReference(storage);
   this.enabled = false;
  }
 }
 
 public class B: MonoBehaviour {
  StorageClass myStorageClass;
  
  public void setStorageReference(StorageClass storage) {
   myStorageClass = storage;
  }
 
  void doSomething() {
   Debug.Log(storage.thisVar); //will be 5 if not changed elsewhere
  }
 }
 
 public class StorageClass {
  public int thisVar;
  public float thatVar;
  public string anotherVar;
 }

Comment
Add comment · Show 8 · 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 Jacob_Anthony · Feb 13, 2017 at 05:01 PM 0
Share

right but i have 1 question: if there will be 2 identical objects this variables can be store in same class (this storage class is attached to empty gameobject or its attached to the same object as those scripts) ? anyway thanks :D

avatar image Chikari Jacob_Anthony · Feb 13, 2017 at 05:03 PM 0
Share

You can use the same class prototype, but you have to create an individual object of type StorageClass within each object. Call storage = new StorageClass(); in the Start function of each object to create a new object.

avatar image Jacob_Anthony Chikari · Feb 13, 2017 at 05:20 PM 0
Share

but storage class is attached to other gameobject or to object with those scripts?

Show more comments
avatar image jdean300 Jacob_Anthony · Feb 13, 2017 at 05:05 PM 0
Share

Yes they can be stored in the same class. In the example @Chikari gave, A and B are holding a reference to the same instance of the StorageClass. In the example, A calls B.setStorageReference to give it the same reference.

avatar image Jacob_Anthony · Feb 13, 2017 at 06:35 PM 0
Share

i am confused by this part

 GetComponent < B > ().enabled = true;
 GetComponent < B > ().setStorageReference(storage);

i tried to get access to scripts by this way with or without adding B script to object but it doesn't work.

avatar image jdean300 Jacob_Anthony · Feb 13, 2017 at 06:39 PM 0
Share

You have to have the B script on the object already. If you want to add one programmatically: B compB = gameObject.AddComponent<B>();

avatar image Jacob_Anthony jdean300 · Feb 13, 2017 at 06:55 PM 0
Share

still don't work this is the error: Expression denotes a method group', where a variable', value' or type' was expected

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

9 People are following this question.

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

Related Questions

How make the same variable have different values for each object ? 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

fix script to get C# var from another object instead of object this script is attached to 2 Answers

change a gameobject into 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