- Home /
How to associate underlying data structure to prefab at runtime?
New to Unity but have programmed. I'm trying to create an Oculus Rift app that visualizes a directory structure, like in a computer, as a tree of nodes. My underlying data structure is, yes, a tree. I have a node class that stores as attributes the file/directory name and the parent node. A sphere in the view represents the node and so there needs to be a way to associate the node to its sphere; The spheres and nodes will be created dynamically during runtime to represent the creation or destruction of the directory's files over time.
The issue I'm having is that I can attach the sphere prefab to my GameManager and get it to show up just fine. But if I attach the sphere to the Node class or vice versa, I get the ArgumentException: The prefab you want to instantiate is null. I've tried multiple ways of associating the Node and sphere with no luck. Any ideas?
Also, if the tree will be growing and shrinking during runtime, will this be an issue of adding animation too?
public class GameManager : MonoBehaviour {
public GameObject sphere;
void Start(){
Node parent = createNode(null, new Vector3(0,0,0));
createNode(parent);
}
// creates node at parent location + offset of parent_vector + new Vector()
public Node createNode(Node parent){
// This works fine to create a sphere
// Instantiate (sphere, parent_vector + new Vector(10,10,10), Quaternion.identity);
// This doesn't work to create a sphere
Node node = new Node(coordinates, parent);
}
public class Node : MonoBehaviour {
public string filename { get; set; }
public Node parent { get; set; }
public GameObject sphere;
public Node(){}
// Just one of the ways i've attempted "associating" or creating the sphere in Node class
public Node(Vector3 coordinates, Node parent){
this.parent = parent;
makeSphere(coordinates);
}
public GameObject makeSphere(Vector3 coordinates){
GameObject clone = (GameObject)Instantiate(sphere, coordinates, Quaternion.identity);
return clone;
}
}
$$anonymous$$ake sphere
your prefab/template and attach Node
script to it first before dragging to Game$$anonymous$$anager
.
public Node sphere;
then
None node = (Node)Instantiate (sphere, parent_vector + new Vector(10,10,10), Quaternion.identity);
and node
is now your new Node
instance.
Or you can .GetComponent< Node >
on the Instantiate
d object.
Answer by Woj_Gabel_FertileSky · Mar 05, 2015 at 10:57 AM
I think that your problem is that you are trying to add a Node that is not a monobehaviour to a gameobject? If you want to store some values with a gameObject, you need to add that to a monobehaviour variable. Also you cannot use New with monobehaviour.
public class MakeObject : MonoBehaviour
{
public GameObject spherePrefab;
public void Start()
{
GameObject go = MakeSpheare(new Vector3(0,0,0));
Node n = go.AddComponent<Node>();
n.someValue = "some text here";
}
public GameObject MakeSpheare(Vector3 coordinates)
{
return (GameObject)Instantiate(spherePrefab, coordinates, Quaternion.identity);
}
}
public class Node : MonoBehaviour
{
public string someValue = "";
}
Your answer
Follow this Question
Related Questions
Spawning a prefab at another object's location 3 Answers
gameObject are not referenced 2 Answers
How do I make prefab follow a gameobject?(C#) 1 Answer