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 dfghdh · Dec 09, 2012 at 04:51 PM · c#gameobjectprojectilecomponents

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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 dfghdh · Dec 09, 2012 at 08:58 PM 0
Share

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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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