- Home /
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.
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.
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);
}
}
}
}
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>();
}
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.
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.