Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 rxdt · Mar 05, 2015 at 10:32 AM · c#gameobjectinstantiateprefabconstructor

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;
     }
 }
Comment
Add comment · Show 1
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 RudyTheDev · Mar 05, 2015 at 11:52 PM 0
Share

$$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 Instantiated object.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 = "";
 }
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

3 People are following this question.

avatar image avatar image avatar image

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

Trouble with destroying an instantiated prefab 2 Answers

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 0 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