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 Hannibal_Leo · Feb 11, 2017 at 03:26 PM · c#scripting problemadvanced

List myClass where MyClass : ScriptableObject not working

Hey Guys,

I obviously don't know how to solve this

I have 3 Classes A, B and C

C : B

B : ScriptableObject

A : MonoBehaviour

in A theres a List

 List<B> bList = new List<B>();

now I want to put C into this List with it's own values

     public class A : MonoBehaviour
         {
         List<B> bList = new List<B>();
         public void AddToList(B bElement) //where I want to add C (or even D : B)
                      // maybe I could call this function this way: A.AddToList(C);
            {
            //and there is the problem - I have to create an Instance of C
            //with 
            //bList.Add(null);
            //bList[0] = (C) ScriptableObject.CreateInstance(typeof(C));
            //but if I do this, no values of C are passed and also it could be D instead of C
            }
         
         }

Does anyone know how to solve this?

Thanks in advice.

Comment
Add comment · Show 3
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 RobAnthem · Feb 11, 2017 at 04:45 PM 0
Share

if B and C derive from the same thing then the list will accept either. If they do not, then only a List<object> will work. Then you can parse out the data like this.

 if (myList[i].GetType() == typeof(objectB)
 {
     (myList[i] as objectB).BProperty1 = 42;
 }
avatar image CarterG81 · Feb 11, 2017 at 07:24 PM 0
Share

@RobAnthem Hi Rob, I am new to Unity answers so I dont know the social norms here. Would it be acceptable for me to add this to my answer to make it more thorough? Or is that inappropriate and it should just remain as only your comment?

avatar image RobAnthem CarterG81 · Feb 11, 2017 at 07:42 PM 0
Share

Yeah of course :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by CarterG81 · Feb 11, 2017 at 03:39 PM

If C derives from B

C : B

Then you should be able to automatically put it into any container that holds B.

 C myC:
 bList.Add (myC);

This is useful when you have methods in B that are overridden in C.

Class B

 SetupObject ()
 {
     //assign variables of B
 }

Class C : B

 override SetupObject ()
  {
      //assign variables of B
      //assign variables of C
  }


@RobAnthem had some great advice:

For objects that DON'T derive from the same base class

if B and C derive from the same thing then the list will accept either. If they do not, then only a List will work. Then you can parse out the data like this. if (myList[i].GetType() == typeof(objectB) { (myList[i] as objectB).BProperty1 = 42; }

ScriptableObject

Unity can't serialize non-Unity based objects. If you want to serialize this use binary or convert it into a ScriptableObject. The main thing about ScriptableObject though is that it requires each instance is created AND stored. Therefore you would need to do something like this.

 AssetDatabase.CreateAsset(newRoomObject, "Assets/Resources/Rooms/" + newRoomObject.RoomName + ".asset");
  RoomObjects tempRoom;
 AssetDatabase.AddObjectToAsset(tempRoom = CreateInstance<RoomObjects>(), "Assets/Resources/Rooms/" + newRoomObject.RoomName + ".asset");
 newRoomObject.requiredObjects.Add(tempRoom);

100% Credit to @RobAnthem for the above. Thank you!

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 Hannibal_Leo · Feb 11, 2017 at 06:31 PM 0
Share

I get this warning when I do so:

 Test3 must be instantiated using the ScriptableObject.CreateInstance method ins$$anonymous$$d of new Test3.
 UnityEngine.ScriptableObject:.ctor()
 Test2:.ctor()
 Test3:.ctor()
 TestEditor1:OnSceneGUI() (at Assets/Editor/TestEditor1.cs:16)
 UnityEditor.DockArea:OnGUI()



here are my test classes:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Test1 : $$anonymous$$onoBehaviour {
 
     public List<Test2> t2 = new List<Test2>();
 
     public void addToList (Test2 typeToAdd)
     {
         t2.Add(typeToAdd);
     }
 
     // Use this for initialization
     void Start () {
         Debug.Log((t2[1] as Test3).asd);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

(for adding Test3: )

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(Test1))]
 public class TestEditor1 : Editor
 {
     
     void OnSceneGUI()
     {
         Test1 t = target as Test1;
         t.transform.position = new Vector3($$anonymous$$athf.RoundToInt(t.transform.position.x-0.5f) +0.5f, $$anonymous$$athf.RoundToInt(t.transform.position.y), $$anonymous$$athf.RoundToInt(t.transform.position.z- 0.5f)+ 0.5f);
         
         Handles.BeginGUI();
         GUILayout.BeginArea(new Rect(Screen.width - 130, Screen.height - 190, 120, 500));
         Test3 testClass = new Test3();
         
         if (GUILayout.Button("Add Field"))
             t.addToList(testClass);
         GUILayout.EndArea();
         Handles.EndGUI();
 
 
     }
 
     
 
 }


Test2

 using UnityEngine;
 using System.Collections;
 
 public class Test2 : ScriptableObject {
 
     int val = 1;
 }

Test3

 using UnityEngine;
 using System.Collections;
 
 public class Test3 : Test2 {
 
     public int asd = 2;
 }
 


avatar image CarterG81 · Feb 11, 2017 at 07:29 PM 0
Share

@hannibal_leo

Why is Test2 derived from ScriptableObject?

avatar image CarterG81 · Feb 11, 2017 at 09:07 PM 0
Share

@Hannibal_Leo Updated the answer to include Rob's advice about ScriptableObject. Perhaps this will help?

avatar image Hannibal_Leo · Feb 12, 2017 at 12:27 AM 0
Share

Still theres the error I just wan't to get rid of it

All classes derive from the Class B - object is not necessary I just want to remove the error message

 Test3 must be instantiated using the ScriptableObject.CreateInstance method ins$$anonymous$$d of new Test3.
  UnityEngine.ScriptableObject:.ctor()
  Test2:.ctor()
  Test3:.ctor()
  TestEditor1:OnSceneGUI() (at Assets/Editor/TestEditor1.cs:16)
  UnityEditor.DockArea:OnGUI()
 
 
avatar image RobAnthem Hannibal_Leo · Feb 12, 2017 at 12:36 AM 0
Share

if you have a list of objects that derive from scriptable object, it is best to make a base class like

 public class classA : ScriptableObject
 {
 }
 
 public class classB: classA 
 {
 }
 
 public class classC: classA 
 {
 }
 
 public class classD : $$anonymous$$onobehaviour
 {
    public List<classA> aList;
    public void CreateAndAdd()
    {
       aList.Add(CreateInstance<classB>());
    }
    public void AddToList(classA a)
    {
       aList.Add(a);
    }
 }
avatar image Hannibal_Leo RobAnthem · Feb 12, 2017 at 12:47 PM 0
Share

RobAnthem

CreateInstance() will not get the values nor the type of the classes I want to add I want to use the function CreateAndAdd() with a specific class Like classB = Helmet ; Helmet can have Helmet.health = 5; or Helmet.health = 7; so I have to add the parameters as well as the class so the function has to look like: CreateAndAdd( theClassToAddWithItsValues) { aList.Add(theClassToAddWithItsValues); //not just the plain class I get when I use CreateInstance, also I don't know the exact type, just the baseclass }
Show more comments

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script will be found on the client but not on the server. 0 Answers

How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers

My sphere is not moving in the unity? 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