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
1
Question by Zhaddul · Nov 23, 2016 at 07:33 AM · instantiatescriptableobject

Instantiating a GameObject from a ScriptableObject

Hi, I am currently working on a system where the main character can summon/instantiate AI from a library, which is held in a scriptableObject. I get a NullRefExeption error.

This is the scriptable object.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DatabaseOfMinions : ScriptableObject {
 
     public Minions[] minions;
 
 }
     //what is in our database
     [System.Serializable]
     public class Minions
     {
         public string name;
         public GameObject prefab;
         public Sprite prefabSprite;
         public Rigidbody body;
 
         public int minionIndex;
         public int damageBlockingIndex;
 
         public float health;
         public float detectionRadius;
         public float attackRadius;
         public float attackDamage;
         public float speed;
         public float jumpHeight;
 
     }
 

This is the script that i use to instantiate:

 using UnityEngine;
 using System.Collections;
 
 public class MinionsSummoningManagerScript : MonoBehaviour {
 
     public DatabaseOfMinions database;
     //public Transform spawnPositionOnLeft;
     public Transform spawnPositionOnRight;
 
     public bool canSummon = true;
 
     void Awake()
     {
         if (!database)
         {
             database = Resources.Load("MinionDatabase") as DatabaseOfMinions;
         }
     }
 
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown("a"))
         {
             if (canSummon)
             {
                 GameObject obj = Instantiate(database.minions[1].prefab, spawnPositionOnRight.transform.position, Quaternion.identity) as GameObject;
             }
         }
     
     }
 }

Any help would be much appreciated. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RX187 · Aug 07, 2018 at 06:50 PM

You may have figured this out by now but I tested your code and it actually works:

  1. On the DatabaseOfMinions script add [CreateAssetMenu(menuName = "New Game")] this will create a menu when your right click in the inspector and you can add the scriptable object. ;

  2. Move the scriptable object to the Resources folder as your path indicates.;

  3. Put the MinionsSummoningManagerScript script on a game object in the scene;

  4. Grab the scriptable object that you placed in the Resources folder and place it on the empy slot in the empty game object you created.

  5. Hit play and it should work.

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

92 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

Related Questions

Can't drag objects into button prefabs to enable OnClick() 0 Answers

Scriptableobject delay for AoE skill which instantiates multiple objects; 1 Answer

C# Add Unique Listener to Buttons Instantiated from List 0 Answers

My code is instantiating many prefabs, i only want one. 1 Answer

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 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