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 /
  • Help Room /
avatar image
0
Question by jintaenate · Jul 25, 2019 at 08:43 AM · scripting problemclassmethod

How to use only 1 method for 2 objects which different class type

I have two objects, the difference is that there are two other classes, and both classes are inherited from the object class. i made code which can create, delete, save or load. So both objects are created, deleted from world, saved or loaded to asset by method

this is for object a

             void CreateObject()
             {
                 PersistableObject instance = Instantiate(persistablePrefab);
                 Transform t = instance.transform;
                 t.localPosition = Random.insideUnitSphere * 5f;
                 t.localRotation = Random.rotation;
                 t.localScale = Vector3.one * Random.Range(0.1f, 1f);
                 persistablePrefabs.Add(instance);
             }

and this is for object b

         void CreateShape()
         {
             ShapeOfPersistableObject instance = shapeFactory.GetRandom();
             Transform t = instance.transform;
             t.localPosition = Random.insideUnitSphere * 5f;
             t.localRotation = Random.rotation;
             t.localScale = Vector3.one * Random.Range(0.1f, 1f);
             varietyShapePrefabs.Add(instance);
         }

As you can see that both methods very similar, only different thing are list and class type

my question is how can i use only 1 method for 2 different class type objects

this is my full script

  public class PersistableObjectsManager : PersistableObject
     {
         public KeyCode createKey = KeyCode.C;
         public KeyCode newGameKey = KeyCode.N;
         public KeyCode saveKey = KeyCode.S;
         public KeyCode loadKey = KeyCode.L;
 
         public PersistentStorage storage;
         public enum ScriptType
         {
             PersistableObject, ShapeVariety
         }
         public ScriptType scriptType;
 
         [Header("Persistable Object")]
 
         public PersistableObject persistablePrefab;
         List<PersistableObject> persistablePrefabs;
 
 
         [Header("Shape Variety")]
 
 
         public ShapeFactory shapeFactory;
         List<ShapeOfPersistableObject> varietyShapePrefabs;
 
 
         void Awake()
         {
             persistablePrefabs = new List<PersistableObject>();
             varietyShapePrefabs = new List<ShapeOfPersistableObject>();
         }
         void Update()
         {
             switch (scriptType)
             {
                 case ScriptType.PersistableObject:
                     InputMethodForPersistableObject();
                     break;
                 case ScriptType.ShapeVariety:
                     InputMethodForObjectsVariety();
                     break;
                 default:
                     break;
             }
 
         }
         #region object
         private void InputMethodForPersistableObject()
         {
             if (Input.GetKeyDown(createKey))
             {
                 CreateObject();
             }
             else if (Input.GetKeyDown(newGameKey))
             {
                 BeginNewGameForObject();
             }
             else if (Input.GetKeyDown(saveKey))
             {
                 storage.Save(this);
             }
             else if (Input.GetKeyDown(loadKey))
             {
                 BeginNewGameForObject();
                 storage.Load(this);
             }
         }
 
         void CreateObject()
         {
             PersistableObject instance = Instantiate(persistablePrefab);
             Transform t = instance.transform;
             t.localPosition = Random.insideUnitSphere * 5f;
             t.localRotation = Random.rotation;
             t.localScale = Vector3.one * Random.Range(0.1f, 1f);
             persistablePrefabs.Add(instance);
         }
 
         void BeginNewGameForObject()
         {
             for (int i = 0; i < persistablePrefabs.Count; i++)
             {
                 Destroy(persistablePrefabs[i].gameObject);
             }
             persistablePrefabs.Clear();
         }
         #endregion
 
 
 
         #region Shape
         private void InputMethodForObjectsVariety()
         {
             if (Input.GetKeyDown(createKey))
             {
                 CreateShape();
             }
             else if (Input.GetKeyDown(newGameKey))
             {
                 BeginNewGameForShape();
             }
             else if (Input.GetKeyDown(saveKey))
             {
                 storage.Save(this);
             }
             else if (Input.GetKeyDown(loadKey))
             {
                 BeginNewGameForShape();
                 storage.Load(this);
             }
         }
         void CreateShape()
         {
             ShapeOfPersistableObject instance = shapeFactory.GetRandom();
             Transform t = instance.transform;
             t.localPosition = Random.insideUnitSphere * 5f;
             t.localRotation = Random.rotation;
             t.localScale = Vector3.one * Random.Range(0.1f, 1f);
             varietyShapePrefabs.Add(instance);
         }
 
         void BeginNewGameForShape()
         {
             for (int i = 0; i < varietyShapePrefabs.Count; i++)
             {
                 Destroy(varietyShapePrefabs[i].gameObject);
             }
             varietyShapePrefabs.Clear();
         }
         #endregion
 
 
         public override void Save(GameDataWriter writer)
         {
             switch (scriptType)
             {
                 case ScriptType.PersistableObject:
                     //before save, need to open file via File.Open
                     writer.Write(persistablePrefabs.Count);
                     for (int i = 0; i < persistablePrefabs.Count; i++)
                     {
                         persistablePrefabs[i].Save(writer);
                     }
                     break;
                 case ScriptType.ShapeVariety:
                     //before save, need to open file via File.Open
                     writer.Write(varietyShapePrefabs.Count);
                     for (int i = 0; i < varietyShapePrefabs.Count; i++)
                     {
                         varietyShapePrefabs[i].Save(writer);
                     }
                     break;
                 default:
                     break;
             }
 
         }
 
 
         public override void Load(GameDataReader reader)
         {
             int count = reader.ReadInt();
             switch (scriptType)
             {
 
                 case ScriptType.PersistableObject:
                     for (int i = 0; i < count; i++)
                     {
                         //PersistableObject @object = Instantiate(persistablePrefab);
                         PersistableObject instance = shapeFactory.Get(0);
                         instance.Load(reader);
                         persistablePrefabs.Add(instance);
                     }
                     break;
                 case ScriptType.ShapeVariety:
 
                     for (int i = 0; i < count; i++)
                     {
                         //PersistableObject @object = Instantiate(persistablePrefab);
                         ShapeOfPersistableObject instance = shapeFactory.Get(0);
                         instance.Load(reader);
                         varietyShapePrefabs.Add(instance);
                     }
                     break;
                 default:
                     break;
             }
         }
     }

Please help me to figure out. thank you

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

0 Replies

· Add your reply
  • Sort: 

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

188 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Function won't be called 1 Answer

How to call a method from a class in a different script in c# 1 Answer

I cant access a script that I made in Assets/Scripts from the FirstPersonCharacter script. 1 Answer

Use Features in a plugin from another plugin 0 Answers

Method not being called from another script 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