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 G-Jewel · Dec 27, 2014 at 04:23 PM · prefabspawnprefabs

Creating A Prefab Loses Hiearchy Connections

Hi there. I have a quick question. I am using a great asset called VectorGrid which works great and can make geometry wars style games.

Anyways the question is nothing to do with the asset but whenever I create a prefab by dragging a object from the Hierarchy to the project folders it loses some of its connections to the objects in the Hierarchy. For example the first picture below shows the enemy cube making a great effect and in order to do that it needs the connection to the grid in the hierarchy. If you see in the script vector grid force it has that connection. The objects in the hierarchy work great.

alt text

However when I use a spawn script to spawn the objects which require a prefab connection to the spawn script the link is lost which result to the effect lost as well. As you see the in the vector grid force the connection is lost.

alt text

I am sure it is something simple to do can someone direct to the right place or provide good instructions? Just want the cube to spawn with the connection so the effect is not lost. Below is the scripts for the spawner and the effect. Do I put something in the void start/awake? Thank you

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour
 {
     public float spawnTime = 5f;        // The amount of time between each spawn.
     public float spawnDelay = 3f;        // The amount of time before spawning starts.
     public GameObject[] enemies;        // Array of enemy prefabs.
 
 
     void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
 
 
     void Spawn ()
     {
         // Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
 
         // Play the spawning effect from all of the particle systems.
         foreach(ParticleSystem p in GetComponentsInChildren<ParticleSystem>())
         {
             p.Play();
         }
     }
 }



 using System.Collections;
 
 public class VectorGridForce : MonoBehaviour 
 {
     public VectorGrid m_VectorGrid;
     public float m_ForceScale;
     public bool m_Directional;
     public Vector3 m_ForceDirection;
     public float m_Radius;
     public Color m_Color = Color.white;
     public bool m_HasColor;
 
     // Update is called once per frame
     void Update () 
     {
         if(m_VectorGrid)
         {
             if(m_Directional)
             {
                 m_VectorGrid.AddGridForce(this.transform.position, m_ForceDirection * m_ForceScale, m_Radius, m_Color, m_HasColor);
             }
             else
             {
                 m_VectorGrid.AddGridForce(this.transform.position, m_ForceScale, m_Radius, m_Color, m_HasColor);
             }
         }
     }
 }
 



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by G-Jewel · Dec 27, 2014 at 06:28 PM

This actually worked it was spelling error. Thank you.

     void start ()
     {
         m_VectorGrid = GameObject.Find("Vector Grid").GetComponent<VectorGrid>();
 
         }
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
avatar image
1

Answer by Owen-Reynolds · Dec 27, 2014 at 04:42 PM

Prefabs can't have connections to objects in the Hierarchy. Of course, once you Instantiate them they aren't prefabs any more -- just regular gameObjects -- so you can have code connect them afterwards. Only the actual prefabs, waiting to be Instantiated, can't have them.

Kind of the reason is that prefabs are for any scene, so can't depend on just one.

Most common work-arounds are to have Start() in the prefab's script set things up. Or do it in your Spawner script, just after Instantiate. The, order, I think is: Instantiate; run Awake for new object; run next lines of your code; run Start of new object.

If the Spawner does it, you can preset all the links in publics, and then just copy them over to the Spawnees.

Comment
Add comment · Show 1 · 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
avatar image Owen-Reynolds · Dec 27, 2014 at 07:53 PM 0
Share

That look about right.

Debug the usual way. Recheck the name. Check the error (is it nullRefException?) Try just GameObject gg=GameObject.Find("Vector Grid"); Debug.Log(gg.name); to be sure to can find just the object.

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

Multiple Cars not working 1 Answer

Spawning objects help 2 Answers

Problem: Random Instantiating more than one prefab? 1 Answer

How can I make an in-game search engine similar to Scribblenauts 1 Answer

How do you instantiate just one prefab? 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