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 Ahsenhein · Mar 19, 2017 at 05:21 PM · scriptableobjectreference

how to reference a list that is inside a scriptable object class from a monobehaviour

I need to create a list of enemies but didn't want to make it in the monobehaviour derived class witch is attached to the enemy because I just need one class(list) for the whole game.

So I created a scriptable object and created the list there. Now I need to add in this list the game object that was instantiated (or delete if it was destroied). Although I'm having a hard time to make this reference thing Works. Here is the example:

public class EnemyStateController : MonoBehaviour {

 public ListOfActiveEnemiesObj enemyListInstance;

 private void Awake()
 {
     
         ListOfActiveEnemiesObj enemyListInstance = ScriptableObject.CreateInstance(typeof(ListOfActiveEnemiesObj)) as ListOfActiveEnemiesObj;


 }

 private void Start()
 {   
     enemyListInstance.addToList(this);

 }

}

public class ListOfActiveEnemiesObj : ScriptableObject {

 public List<GameObject> enemyList;
 
 public void addToList(EnemyStateController enemy)
 {
     enemyList.Add(enemy.gameObject);
     Debug.Log(enemyList);
 }

}

So basically I need to connect both classes so I can call the function addToList()

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
0
Best Answer

Answer by Commoble · Mar 19, 2017 at 05:27 PM

ScriptableObjects aren't meant to be edited at runtime, and if you need to edit your data during runtime, then using a ScriptableObject is not a good solution to the problem.

There's a few ways to do what you need to do. If you need a single, globally accessible data structure that doesn't need references to assets or scene objects until after the game starts, a basic class (not a MonoBehaviour or ScriptableObject) with static fields works well:

 public class EnemyManager
 {
     public static List<GameObject> enemyList = new List<GameObject>();
 
     public static void addToList(EnemyStateController enemy)
     {
         enemyList.Add(enemy.gameObject);
         Debug.Log(enemyList):
     }
 }
 
 // then in your other script
 
 public EnemyStateController : MonoBehaviour
 {
     private void Start()
     {
         // you don't need a reference to use public static fields
         EnemyManager.addToList(this);
     }
 }
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 Ahsenhein · Mar 19, 2017 at 06:48 PM 0
Share

Thank you for the answer.

Let me get it right then. Scriptable object is used when we don't need to change anything inside of the class and so they are unchangeable, therefore they are not instantiated because, if I get it right, they are a static global piece of data, right? So a good use of them is if many objects(like an enemy) share the same mechanic, so their monobehaviour can call the scriptable object, pass the reference and then do what they need to do and return values for each of them. Also, if I need a script that doesn't need to be instantiated and can be used by other objects but need to be able to change some data then a basic class can be used for it?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Losing reference to monobehaviour after updating a dll 0 Answers

How do you save a reference to a prefab's script in a ScriptableObject? 0 Answers

Unity Editor Animating object reference. 1 Answer

Pass ScriptableObject field into a function by reference 2 Answers

Having a reference to only one variable. 2 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