Little tweak at Shooter.cs

Hey

I have don something different in the Shooter.cs file.
I didn’t use the public GameObject gun that Ben uses, but a private Transform gun.
So in this case, I had not to drag and drop the GameObject into the inspector.

I have one little question: why is it public variable gun a GameObject and the private gun a Transform?

This is the code, that seems to work:

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

	public GameObject projectile, projectileParent;
	
	private Transform gun;
		
	// Use this for initialization
	void Start () {
		gun = gameObject.transform.FindChild("Gun");
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	private void Fire () {
		// Parent the projectile to the Projectiles in the Hierarchy
		GameObject newProjectile = Instantiate(projectile) as GameObject;
		newProjectile.transform.parent = projectileParent.transform;
		
        // Spawn projectile on gun position 
		newProjectile.transform.position = gun.transform.position;
	}
}

Privacy & Terms