- Home /
Manually creating "nested" gameObjects
I need a game object to instantiate projectiles at that is attached to the one that is firing it. However there are multiple NPCs as well as the player that have the ability to fire spells.
The ability of that NPC/Player needs to have reference to its own spell start game object. However this spell start object has to move around with the NPC/Player and so the game objects needs to be attached to the NPC/Player game object.
I need a way to either include this object in the prefab I create my NPCs from and then use a command to return game objects attached to other game objects by name or I need to be able to create the spell start game object at rune time and attach it to the NPC/Player game object.
How might I go about doing such a thing? Basically it is currently as follows: I have the player and the casting NPC and both are tying to use the same spell start object to instantiate their projectiles at.
Answer by Bunny83 · Dec 09, 2012 at 07:33 PM
You want attach projectiles which you fire to your moving and rotating character as child objects? That's not really a good approach since the projectiles would rotate / move along with the player. You should create that object in the scene, Your character can hold a reference to the gameobject.
To create an empty gameobject, just do this:
GameObject newGO = new GameObject("Name Of GameObject");
Just declare a private variable and store the gameobject reference there. So you can easily attach your projectiles to the gameobject, but the gameobject itself is stationary.
private Transform projectileContainer;
void Start()
{
projectileContainer = (new GameObject(name + "_projectiles")).transform;
}
void OnDestroy()
{
if (projectileContainer != null)
Destroy(projectileContainer.gameObject);
}
In this case i directly store the reference to the transform of the GameObject which you need when you want to parent other objects to this one. The name of the GameObject is the same as the name of the gameobject this script is attached to + "_projectiles".
The OnDestroy will destroy the container object when the actual player object is destroyed
This wasn't actually my issue. Also I didn't want to child my actual projectile to the character. I wanted to child a transform so i knew where to instantiate the projectile from. I solved this problem by storing the spell start game object in my script.
using UnityEngine;
using System.Collections;
public class Fireball : Ability {
GameObject _fireball;
GameObject _spellStart;
public Fireball(GameObject instigator)
{
_id = (int)ID.FIREBALL;
_name = "Fireball";
_fireball = (GameObject)Resources.Load("FireBall", typeof(GameObject));
_spellStart = instigator.transform.Find("SpellStart").gameObject;
_icon = (Texture2D)Resources.Load("FireBall", typeof(Texture2D));
_baseCoolDown = 3.5;
_coolDownLeft = _baseCoolDown;
_onCoolDown = false;
_baseDamage = 15;
_int$$anonymous$$odifier = 2.5;
_strength$$anonymous$$odifier = 0;
_stamnia$$anonymous$$odifier = 0;
_range = 30.0f;
_cone = 0.6f;
}
public override void Use(Attribute att, Transform target, Transform instigator, Vector3 direction)
{
double _distance = Vector3.Distance(target.position, instigator.position);
Vector3 _direction = (target.transform.position - instigator.position).normalized;
double _dir = Vector3.Dot(_direction, instigator.forward);
if(!_onCoolDown)
{
if (_distance < _range)
{
if (_dir > _cone)
{
_onCoolDown = true;
GameObject track = (GameObject)GameObject.Instantiate(_fireball, _spellStart.transform.position, _spellStart.transform.rotation);
track.GetComponent<Tracking>().Damage = CalculateDamage(att);
track.GetComponent<Tracking>().Target = target;
}
}
else
_error = "Out of range";
}
else
_error = "On Cooldown";
}
}
As you can see this gets the game object childed to the transform of the parent.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Projectile gets stuck at character's position (Unity2d) 0 Answers
Check for empty gameobject in script? 1 Answer
Multiple Cars not working 1 Answer
How to add a Script to a GameObject during Runtime [2016] 3 Answers