- Home /
 
How to Instantiate a GameObject from a ScriptableObject piece of script?
I've started transferring a 3D space game to Unity and I'd like to clone multiple copies of my 'Planet' prefab, from a ScriptableObject piece of code. Here's the StarSystemDetails class:
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class StarSystemDetails : ScriptableObject {
     public int numPlanets;
     private List<SystemBodyDetails> _systemBodies = new List<SystemBodyDetails>();
     public Vector3 coordinates;
     public SystemBody planet;
 
     // class constructor
     public StarSystemDetails() {
         numPlanets = 4;
     }
 
     public void Init(string p_name) {
         name = p_name;
     }
 
     void Start() {
         float _orbitDistance = 1000;
         for (int x = 0; x < numPlanets; x++) {
             SystemBodyDetails _systemBodyDetails = new SystemBodyDetails(_orbitDistance);
             _systemBodies.Add(_systemBodyDetails);
             
             SystemBody newPlanet = Instantiate(planet, _systemBodyDetails.position(), _systemBodyDetails.rotation()) as SystemBody;
             Debug.Log("new planet = "+newPlanet);
             // pass SystemBodyDetails into newPlanet...
             //newPlanet.GetComponent<SystemBody>.init(_systemBodyDetails);
             
             _orbitDistance += 1000;
         }
     }
 }
 
               This is all very early-stage at this point; I'm just trying to get 4 Planets appearing in my scene. At the moment they aren't and I do not know why.
I'm aware that the class has not been 'passed' a reference to the Planet prefab, but I don't see how to drag and drop it component-style into a ScriptableObject in the editor. I'm assuming that's not possible. At the moment I'm using SystemBody as that's the piece of code that's a component on the Planet prefab. I know that must be wrong. But how do I Instantiate a prefab GameObject from a ScriptableObject?
Does anyone have any ideas what I'm doing wrong? And how to do it right?
Thanks.
Your answer