when firing projectile, it fires from all instances of the gameobject?

Hello again! I feel as though I have come up with a solution to my problem, however I cannot find an adequate way to script the actions necessary.

The Basis: I am spawning a projectile from a specific location called “MagicStart” which works perfectly.

The Issue: it only works perfectly for one character. when I have 3 characters in the party every time I fire, it fires from all 3 character’s MagicStart.

The Solution: I believe I can tag the active character as “playerControlling” and have the script find the child object “MagicStart” only on the player that is currently tagged “playerControlling.”

My Question: How would I go about scripting locating the specific child under the player tagged as above?

All 3 characters have the potential to be controlled by the player so it will constantly be changing, and when not being controlled by the player it will be controlled by a separate AI script.

Current firing script:

using UnityEngine;
using System.Collections;

public class FireProjectile : MonoBehaviour {

    RaycastHit hit;
    public GameObject[] projectiles;
    Transform spawnPosition;
   // [HideInInspector]
	public int currentProjectile = 0;


	void Start () {
		spawnPosition = GameObject.FindGameObjectWithTag ("magicStart").transform;
	}
	
	// Update is called once per frame
	void Update () {

        if (Input.GetButtonDown("Fire1"))
        {
			if (Physics.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), out hit, 100f))
            {
                GameObject projectile = Instantiate(projectiles[currentProjectile], spawnPosition.position, Quaternion.identity) as GameObject;
                projectile.transform.LookAt(hit.point);
                projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * 1000);
                projectile.GetComponent<ProjectileScript>().impactNormal = hit.normal;
            }
        }
       	}

    public void nextEffect()
    {
        if (currentProjectile < projectiles.Length - 1)
            currentProjectile++;
        else
            currentProjectile = 0;
    }

    public void previousEffect()
    {
        if (currentProjectile > 0)
            currentProjectile--;
        else
            currentProjectile = projectiles.Length-1;
    }
}

You could add a property to your script like this:

public bool playerControlled

set it to true when the character is being controlled by the player. Then in update:

if (Input.GetButtonDown("Fire1") && playerControlled)
{
if (Physics.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), out hit, 100f))
{
GameObject projectile = Instantiate(projectiles[currentProjectile], spawnPosition.position, Quaternion.identity) as GameObject;
projectile.transform.LookAt(hit.point);
projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * 1000);
projectile.GetComponent<ProjectileScript>().impactNormal = hit.normal;
}
}

to improve on this, move the fire code to a seperate public method so that your AI can call it explicitly:

public void Fire() {
if (Physics.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), out hit, 100f))
    {
    GameObject projectile = Instantiate(projectiles[currentProjectile], spawnPosition.position, Quaternion.identity) as GameObject;
    projectile.transform.LookAt(hit.point);
    projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * 1000);
    projectile.GetComponent<ProjectileScript>().impactNormal = hit.normal;
    }
}

Try using -

if (Input.GetButtonDown("Fire1") && (other.tag == "PlayerControlling"))
		{
			//Do your stuff here !
		}

Should work.