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 Fire_Mage_Matt · Mar 20, 2020 at 12:51 PM · scriptableobjectscriptable objectaccessing scripts

How to access a list from a scriptable object?

I am in the process of making a game and currently trying to have a message appear that will state which enemy has appeared and where the battle is taking place. The various areas of the game are saved as scriptable objects. In each area I have saved a list of enemies and a list of allies.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu(fileName = "NewArea", menuName = "Area")]
 public class Areas : ScriptableObject
 {
     public List<ScriptableObject> enemy;
     public List<ScriptableObject> allies;
 }

When the battle starts, the battle creation script goes through a list of areas and finds the one you are currently in. The script then will display the message "A wild ----- appeared in the -----". I can get the area name to appear easily enough, but I am lost when it comes to finding the enemy name from the scriptable object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class BattleCreation : MonoBehaviour
 {
     public Text discriptionText;
     public List<ScriptableObject> areas;
     public ScriptableObject CurrentArea;
 
     void Start()
     {
         //Find Current Area
         Scene scene = SceneManager.GetActiveScene();
         for (int i = 0; i < areas.Count; i++)
         {
             if (scene.name == areas[i].name)
             {
                 CurrentArea = areas[i];
             }
         }
         //Display text (current issue)
         discriptionText.text = "A wild " + CurrentArea.enemy[0] + " appeared in the " + CurrentArea.name;
     }
 }
Comment
Add comment · Show 5
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 ShadyProductions · Mar 20, 2020 at 01:17 PM 0
Share
 CurrentArea.enemy
 CurrentArea.allies

I'm not sure what the problem is?

avatar image Fire_Mage_Matt ShadyProductions · Mar 20, 2020 at 04:17 PM 0
Share

The original issue I had was that whenever I tried to use CurrentArea.enemy I would get this:

 Assets\Scripts\BattleCreation.cs(25,56): error CS1061: 'ScriptableObject' does not contain a definition for 'enemy' and no accessible extension method 'enemy' accepting a first argument of type 'ScriptableObject' could be found (are you missing a using directive or an assembly reference?)
avatar image ShadyProductions Fire_Mage_Matt · Mar 20, 2020 at 04:20 PM 0
Share

You need to pass not scriptableobject put the class name of your scriptable object so Areas

 public List<Areas> areas;
avatar image Skaltum · Mar 20, 2020 at 04:04 PM 0
Share

Are the allies / enemies supposed to be scriptable objects themselves? You probably want to replace that with something more specific.

 public List<Enemy> enemies; // ins$$anonymous$$d of List<ScriptableObject>
 //that would allow you to interact with the public members of your enemy class.


 foreach(var enemy in CurrentArea.enemy) {
     descriptionText.text += $"{enemy.name} appeared\n";
 }

That will just print the names of the scriptable objects you assigned in the area.

avatar image Fire_Mage_Matt Skaltum · Mar 20, 2020 at 04:16 PM 0
Share

The enemies are scriptable objects themselves. When I declare the list like you did I get this error:

 Assets\Scripts\Areas.cs(8,17): error CS0246: The type or namespace name 'Enemy' could not be found (are you missing a using directive or an assembly reference?)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zentiu · Mar 21, 2020 at 12:16 AM

You can not change or look up any value in a scriptableObject unless you instantiate or in this case create a new instance of it first. The instance of the scriptableObject will have all the values for that instance that you set as a scriptable object and can then manipulate it. I personally dont use scriptable objects unless i am creating like items for the game. For enemies i dont do that but it is possible. Look at scriptableObjects as prefabs. instantiate them first and then you can manipulate any value you set access for.

If you need more let me know.

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

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

129 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

Related Questions

How to Update ScriptableObject 1 Answer

How can I find all instances of a Scriptable Object in the Project (Editor) 3 Answers

Are ScriptableObjects good for non-visualised models? 2 Answers

Instancing ScriptableObject not saved in assets results in warning message 0 Answers

Why do I need to serialize a struct inside a ScriptableObject to save its data? 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