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 Yinoguns · Jun 20, 2013 at 01:01 PM · childtagreferenceweaponaccess

Using Tag to find Child and execute Scripts

Hello, I am in University and I have learnt Java, but no C# as like a dolt I didnt take the module for it. However next semester I am going to do a module for Unity and maybe my project with Unity.

To come to the point, I am currently not interested in the efficient way to do something, but how to access items and the general flow of C# and Unity; so I set goals for myself and try to work out how to do it, this gains me understanding of the interaction.

The Hierarchy, of the setup is Player -> Weapon Root -> Weapon -> BulletSpawn.

![alt text][1] [1]: /storage/temp/12202-playerspreadimage.png

So what I am doing is I have made the program work so in the arangement depicted above, bullets appear and are provided a force to fly out from the camera.

 if( Input.GetButtonDown("Fire1")    ){
    Camera cam = Camera.main;
    GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position, cam.transform.rotation);
    thebullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);

Nice and simple, now what I want to do is from a script on WeaponRoot, which will handle the current weapon in use; I need a way to say "find this child" ive been trying Tags so I will explain with that. I want the script in Weapon root to say "look for child with Tag Weapon" and then use the reference to the weapon to do the same to find it's child with the Tag 'BulletSpawn'.

What I want to do this for is that I can then move onto having an active weapon, automatically detect that, find its BulletSpawn and fire bullets from its unique barrel.

 WeaponBarrel = transform.FindWithTag("Weapon/BulletSpawn");
                                     
 GameObject thebullet = (GameObject)Instantiate(bullet_prefab, WeaponBarrel.transform.position, WeaponBarrel.transform.rotation);
 thebullet.rigidbody.AddForce( WeaponBarrel.transform.forward * bulletImpulse, ForceMode.Impulse);    

The above is my latest attempt, and provides a long error of: Assets/Scripts/Shoot.cs(35,50): error CS1061: Type UnityEngine.Transform' does not contain a definition for FindWithTag' and no extension method FindWithTag' of type UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)


Last little bit, here is the top bit of my code:

 using UnityEngine;
 using System.Collections;
 
 
 public class Shoot : MonoBehaviour {
     
     public GameObject bullet_prefab;
     float bulletImpulse = 10f;
     
     private GameObject WeaponBarrel;
 
     // Use this for initialization
     void Start () {


Thanks for any help, if possible dont just fix the code if you can, but explain why mine doesnt work and why yours does.

playerspreadimage.png (72.2 kB)
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

Answer by Tarlius · Jun 20, 2013 at 01:06 PM

FindWithTag is a GameObject method, not a Transform method.

Swap the "transform" for "GameObject", as shown in the usage example in the documentation.

Edit: Depending on the type of WeaponBarrel, you may also need to do a GetComponent or add a .transform to the end.

Comment
Add comment · Show 9 · 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 Yinoguns · Jun 20, 2013 at 02:29 PM 0
Share

Thanks, I think I may need to clear some things up. Weapon Barrel is an Empty at the front of the barrel, I recently saw this used on the Tutorials being used for what I am trying to do.

What I am trying to do is ins$$anonymous$$d of attaching the Empty to the script for the spawn, I want to TAG find the Empty and use its position and rotation for the spawning of the bullets.

Any clarity questions, please ask.

avatar image Tarlius · Jun 20, 2013 at 04:31 PM 0
Share

What is an "Empty"? An GameObject with no scripts on it?

In case it wasn't clear, by type I mean like "typeof(WeaponBarrel)". Ie: Transform/GameObject/ArbitraryGunBarrel$$anonymous$$onoBehavior. FindByTag() will return a GameObject, so if WeaponBarrel is not of type GameObject you'll get another compiler error if you don't add a GetComponent.

I feel it probably worth noting that using a tag to find the object would be rather slow, so be sure to cache the result

Either way, you will need to find the GameObject first, and then get whatever you need from it, or have the script register with the class via a singleton method or something.

Since it doesn't sound like you have anything special on the object, you probably are trying to get its transform, which would simply be GameObject.FindByTag("YourTag").transform;, which would be shorthand for

 GameObject weaponBarrelGameObject = GameObject.FindByTag("YourTag");
 Transform weaponBarrel = weaponBarrelGameObject.transform;
avatar image Yinoguns · Jun 20, 2013 at 05:47 PM 0
Share

Ok I get some of that, I am really new to this; by Empty I mean: "GameObject -> Create Empty (Ctrl+Shift+N) "

Just to make sure, I am using C# I forgot to mention that.

Is an Empty not a GameObject?

So what I have tried now is:

//Find the E$$anonymous$$PTY and assign it to an object reference GameObject ObjectTag = GameObject.FindWithTag("BulletSpawn"); //Get the transform component and put that into a Transform object Transform BulletPos = ObjectTag.GetComponent();

//Then use the transform information to fire the bullet. GameObject thebullet = (GameObject)Instantiate(bullet_prefab, BulletPos.transform.position, BulletPos.transform.rotation); thebullet.rigidbody.AddForce( BulletPos.transform.forward * bulletImpulse, Force$$anonymous$$ode.Impulse);

![alt text][1] [1]: /storage/temp/12214-emptyobject_hi.png

For all I know, its not possible, but feels like the kind of thing that should be.

emptyobject_hi.png (227.1 kB)
avatar image Yinoguns · Jun 20, 2013 at 05:55 PM 0
Share

Also, how are you doing the code blocks, the Ctrl+$$anonymous$$ feature on here isn't doing it for me.

avatar image Tarlius · Jun 20, 2013 at 06:01 PM 0
Share

Create Empty will create an empty GameObject, yeah. But there is no "Empty" object, if you know what I mean. Your capitalisation made me think perhaps it was a class is all.

The code looks good, is it not working as expected?

I think you may want to use rigidBody.constant force or perhaps velocity (since a bullet doesn't have a constant force on it), although it will depend what sort of effect you intend to achieve

Show more comments
avatar image
0

Answer by OP_toss · Jun 20, 2013 at 05:44 PM

Like Tarlius said, FindWithTag is not a Transform method, it is for GameObjects.

BUT I'd also like to point out some bigger issues with your code:

Finding objects with tags can be slow. It's also a string-based lookup, requiring string comparisons, which is slow. I suggest looking at the children directly, through the parent's transform.

 transform.GetChildAt(0).gameObject; //get the first child's gameobject

This avoids having wierd redundant hierarchical information in Tags. It's also much faster as a lookup. If there's multiple children you can loop through children, or use a GetComponentInChildren if there's a specific type of child you're looking for.

Also, member variables in C# are never capitalized. Only Classes/Structs/Enums and the like are. So I'd change WeaponBarrel to weaponBarrel.

Hope this helps!

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 Tarlius · Jun 20, 2013 at 05:51 PM 0
Share

But caching the result would be faster still. I kinda assumed he was searching from outside the parent to be needing a tag (otherwise just linking would be easier, no?) but if this is not the case and there is a reason to not just link, then this is sound advice.

The coding guidelines is also sound advice (note I did this in my code snippet to be more subtle :p )

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

16 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding a child without knowing its name? 1 Answer

Best way to Reference Child from Parent's script? 3 Answers

Access a child from the parent or other gameObject. 2 Answers

GameObjectWithTag Child 1 Answer

Set reference to multiple gameobjects based on tag category / multiple tags? 2 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