- Home /
Accessing List from Parent G.O. Script
Hello! I am creating a first person game in which that player currently can cycle through 3 different weapons at start and am trying to make it so the ability to cycle through weapons only becomes active after the player has picked up each weapon pickup. The way it is setup is that the when the player game object collides with the pickup, the weapon is set to active and added to a weaponList on the parent's PickupCollider script. The ability to cycle through weapons is handled in a child empty gameobject of the Player. I seem to be having trouble getting the script with the list into the script that handles weapon cycle.
Am I on the right path so far? Is this the simplest way of accessing the Parent Object's script?
Can anyone inform me on how to actually access the Parent's weaponList once the script?
I have posted the child's weapon cycle script (trying to access the parent script and import the weaponList)
Thanks in advance!
using UnityEngine;
using System.Collections.Generic;
public class WeaponSwitch02 : MonoBehaviour {
//public List<GameObject> weaponList = new List<GameObject>();
public PickupCollider pickupColliderScript;
public int currentWeapon = 0;
public int maxWeapons = 2;
public Animator theAnimator;
public GameObject fistWeapon; //set in inspector
public GameObject pencilWeapon; //set in inspector (not currently affecting weaponswitch 12/30/14 11:30am)
public GameObject pistolWeapon; //set in inspector (not currently affecting weaponswitch 12/30/14 11:30am)
// Use this for initialization
void Awake () {
SelectWeapon (0);
//weaponList = new List<GameObject> ();
}
void Start(){
pickupColliderScript = GameObject.FindObjectOfType (typeof(PickupCollider)) as PickupCollider;
pickupColliderScript.Test();
}
// Update is called once per frame
void Update () {
//if we scroll mousewheel up and down, cycles through weapons
if (Input.GetAxis("Mouse ScrollWheel") > 0){
if (currentWeapon + 1 <= maxWeapons){
//then add 1 to current weapon
currentWeapon ++;
}
else{
currentWeapon = 0;
}
SelectWeapon(currentWeapon);
}
else if(Input.GetAxis ("Mouse ScrollWheel") < 0){
if (currentWeapon -1 >= 0){
currentWeapon --;
}
else{
currentWeapon = maxWeapons;
}
SelectWeapon(currentWeapon);
}
if (currentWeapon == maxWeapons + 1) {
currentWeapon = 0;
}
if (currentWeapon == -1) {
currentWeapon = maxWeapons;
}
//if we press 1 key, current weapon = 0, selectweapon will be called with currentWeapon variable
if (Input.GetKeyDown(KeyCode.Alpha1)) {
currentWeapon = 0;
SelectWeapon(currentWeapon);
}
if (Input.GetKeyDown(KeyCode.Alpha2)){
currentWeapon = 1;
SelectWeapon(currentWeapon);
}
if (Input.GetKeyDown(KeyCode.Alpha3)){
currentWeapon = 2;
SelectWeapon(currentWeapon);
}
}
void SelectWeapon(int index){
//call a piece of code for each child in this object
for (int i = 0; i < transform.childCount; i++) {
//activate selected weapon
if (i == index){
if (transform.GetChild(i).name == "FistWeapon"){
theAnimator.SetBool("WeaponIsOn ", false);
}
else{
theAnimator.SetBool("WeaponIsOn", true);
}
transform.GetChild(i).gameObject.SetActive(true); //find child of transform using i, using gameobject to set it active
}
//if the one we are itterating through = currentWeapon it will keep active, if not it will deactivate it
else{
transform.GetChild (i).gameObject.SetActive(false);
}
}
}
}
It's late so I'm not going to attempt a full answer.
You need to reference the script that holds the list of weapons.
private Weapons theWeaponsScript; // assumes your script is called Weapons
The list needs to be public in that Weapons script so you can access it.
$$anonymous$$eep track of the available list to your player by holding a local list, all that list needs is the position in the main Weapons list that the picked up weapon has.
So the main weapon list has:
0 = hand gun 1 = shotgun 2 = rifle 3 = bazooka!
but if the player picks up a rifle first then a hand gun then the local player List is this:
0 = 2 1 = 0
Then reference back to the original list.
Like I said it's late, I hope that makes sense.
If this is still unanswered tomorrow I'll post a fuller answer.
EDIT:
Should add you need to source the Weapons script so whatever GameObject that script is attached to do a theWeaponsScript = GameObject.Find("GOName").GetComponent();
then you can access the public list with
theWeaponsScript.myList