Question by
Mewkat · Oct 26, 2020 at 06:25 PM ·
gameobjectinstantiatetagmultiple
instantiate prefab at every gameobject with tag
I am trying to modify this code to add multiple hardpoints which will be used to launch projectiles from. Now I want the script to find all the hardpoints by tag and then instantiate the projectile at their location. Here is my script that doesn't work:
public GameObject[] hardpoints;
public GameObject bullet;
void Start() {
if (hardpoints == null)
hardpoints = GameObject.FindGameObjectsWithTag("ProjectileLaunchPoint");
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
fireShot();
}
}
public void fireShot() {
GameObject shot1 = (GameObject)GameObject.Instantiate(bullet, hardpoints.transform.position, Quaternion.identity);
Ray vRay;
if (!CustomPointer.instance.center_lock)
vRay = Camera.main.ScreenPointToRay(CustomPointer.pointerPosition);
else
vRay = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2f, Screen.height / 2f));
RaycastHit hit;
if (Physics.Raycast(vRay, out hit)) {
shot1.transform.LookAt(hit.point);
shot1.GetComponent<Rigidbody>().AddForce((shot1.transform.forward) * 9000f);
} else {
shot1.GetComponent<Rigidbody>().AddForce((vRay.direction) * 9000f);
}
}
The error that I get:
error CS1061: 'GameObject[]' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)
Comment