Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GregoryNeal · Dec 13, 2016 at 05:25 PM · componentmonobehaviourinitializationresources.loadall

When do components of loaded prefabs get initialized?

I have a Resources folder where I store all of my game weapon prefabs. Upon loading the main scene, I store all of those in a list with a Resources.LoadAll call in a WeaponSystem component. Each prefab has a component that derives from a base Weapon class and WeaponSystem swaps between them at runtime, just setting the currentWeapon variable to the new selected weapon.

In my TractorBeam weapon script I've placed two print statements, one in Start, and another in Update, neither of them are being called when the game runs. I'm trying to initialize variables and stuff in these scripts but no Monobehaviour derived method wants to run.

 public class WeaponSystem : MonoBehaviour {

     public GameObject[] installedWeapons;
     private Weapon currWeapon;
     private int currWeapon_index = 0;

     private string resourcesPath = "Prefabs/Weapons/";
     private List<Transform> fireLocations = new List<Transform>();
     private InputMapper im;

     void Awake() {
         installedWeapons = Resources.LoadAll<GameObject>(resourcesPath);
         currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
     }

 // Use this for initialization
 void Start () {
     im = GameObject.Find("GameManager").GetComponent<InputMapper>();

         fireLocations.Add(transform.Find("fire1"));
         fireLocations.Add(transform.Find("fire2"));
 }
 
 // Update is called once per frame
 void Update () {
         if (currWeapon != null) {            
             if (currWeapon.isContinuousFire) {
         if (Input.GetKey(im.currentWeaponMainFireKey)) {
                     ShootPrimary();
                 }
             }
             else {
                 if (Input.GetKeyDown(im.currentWeaponMainFireKey)) {
                     ShootPrimary();
                 }
             }
         }

         if (Input.GetKeyDown(im.nextWeaponKey)) {
             SetNextWeapon();
         }

         if (Input.GetKeyDown(im.previousWeaponKey)) {
             SetPreviousWeapon();
         }
 }

     private void ShootPrimary() {
         foreach (Transform location in fireLocations) { 
             currWeapon.Fire(location);
         }
     }

     private void SetNextWeapon() {
         currWeapon_index = (currWeapon_index+1).Modulo(installedWeapons.Length);
         currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
     }

     private void SetPreviousWeapon() {
         currWeapon_index = (currWeapon_index-1).Modulo(installedWeapons.Length);
         currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
     }

     void OnGUI() {
         GUI.Label(new Rect(10, 150, 300, 20), "Current weapon: " + currWeapon.w_name);
     }
 }


 public class TractorBeam : Weapon {

     public float beamRadius = 5;
     public float beamReach = 20;
     public float beamForce = 2;
     public override bool isContinuousFire {
         get { return true; }
     }
     public override string w_name {
         get { return "Tractor Beam"; }
     }
 
     private PlayerMovement pm;

     void Awake () {
         print ("started");
     }

     // Update is called once per frame
     void Update () {
         print ("in update");        
     }

     public override void Fire(Transform location) {
        ...
     }
 }


 public class Weapon : MonoBehaviour {

     public Dictionary<ResourceType, float> requiredResources = new Dictionary<ResourceType, float>();
     public virtual bool isContinuousFire {
         get { return false; }
     }
     public virtual string w_name {
         get { return "Unnamed weapon"; }
     }

     //Fires a weapon at the Transform location
     public virtual void Fire(Transform location) {

     }

 void Start() {
 }

 void Update() {
     
 }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hrgchris · Dec 13, 2016 at 05:50 PM

Hi there

I think the exact issue with your code is that you aren't instantiating your 'weapon'. You have use resources.load to load the 'prefabs', but you then need to turn those prefabs into actual game objects for them to start/update etc.

Without knowing exactly what behaviour you're after it's hard to write the exact code, but if you wanted each of those weapon prefabs to be instantiated, it'd be something like this:

 public GameObject[] installedWeaponPrefabs;
 public GameObject[] installedWeapons;
 
 
 void Awake() {
      installedWeaponPrefabs = Resources.LoadAll<GameObject>(resourcesPath);
      installedWeapons = new GameObject[installedWeaponPrefabs.Length];
      for(int i = 0; i < installedWeapons.Length; i++)
      {
         installedWeapons[i] = Instantiate(installedWeaponPrefabs[i]);
      }
      
      currWeapon = installedWeapons[currWeapon_index].GetComponent<Weapon>();
  }


That code loads all the prefabs, then creates 1 game object from each one and stores it in the installedWeapons array.

If you do ever want to know about when / how the various unity events are fired, this page is very useful:

https://docs.unity3d.com/Manual/ExecutionOrder.html

As I say though, I don't think ordering is the issue here

Hope that helps

-Chris

Comment
Add comment · Show 1 · Share
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 GregoryNeal · Dec 13, 2016 at 07:01 PM 0
Share

Thanks for the insight! I was looking in the wrong direction with the initialization stuff, when the issue was just having no instance of my prefab! Everything works beautifully now.

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

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Copying a Component's values without a new Game Object 1 Answer

bool field initialization ignored 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Is it possible to mark MonoBehaviour as EditorOnly 2 Answers

Caching a Monobehaviour Component's reference properties (rigidbody2d, specifically) 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