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.

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.

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.

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!