Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Guruguy69 · Dec 30, 2014 at 09:24 PM · c#

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);
             }
         }
     }
 }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Dec 30, 2014 at 09:57 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Call StartCoroutine() from inside a function other than Start!? 2 Answers

how can i auto target Gameobject on startup 1 Answer

How To Make An Invisible Wall That's Impossible To Get Out Of? 1 Answer

Not working gui buttons 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges