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 /
  • Help Room /
avatar image
0
Question by Tageos · Feb 29, 2016 at 06:15 PM · c#array

Getting variables that from an array of scripts

Hello!

My scene has aliens that each has a lifetimer, when an alien is killed a manager script should register the life time of that alien. Right now i cant figure out how to get the independant scripts from the aliens in the manager script. I thought of using an array of scripts but the array is empty everytime i run the scene ...

This is the manager script:

 using UnityEngine;
 using System.Collections;
 
 public class Manager : MonoBehaviour {
 
     public AlienScript[] alienScripts;
     public float[] lifeTimers;
 
     void Start () {
        alienScripts = GetComponents<AlienScript>();
     }
 }

Does anyone have tips on how to get the alien scripts and how to store their life timers in an array?

Thank you.

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 RobGraat · Feb 29, 2016 at 07:13 PM 0
Share

The GetComponents method only returns the AlienScript components attached to the $$anonymous$$anager game object.

Assu$$anonymous$$g that the AlienScript components are attached to seperate Alien game objects then the method would return an empty array. Is that correct?

The first step you then need to make is providing the $$anonymous$$anager with the game objects containing the AlienScript components.

I would suggest using a List, as it can more easily change size when you wish to add more aliens at runtime.

Edit: If you are just making a small test game. Then you can start by creating a number of Alien game objects. Disable them. Then drag all the game objects onto the AlienScripts field. You will then have them preloaded and no longer need to search for them at Start.

2 Replies

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

Answer by Placus_Brutus · Mar 01, 2016 at 08:18 AM

Add this above your "public AlienScript[] alienScripts" declaration:

 private static Manager instance = null;
 public static Manager managerController
 {
     get
     {
         if (instance == null)
         {

             instance = GameObject.Find("NameOfManagerObjectHere").GetComponent<Manager>();
         }
         return instance;
     }
 }

Then, create a new function:

 public void SaveLifeTimers(float time)
 {
     // add to your lifeTimers array here!
 }

Inside your Alien script now, create a new function:

 void SaveTime()
 {
     float lifeTime; // Whatever your time is
     Manager.managerController.SaveLifeTimers(lifeTime);
 }
Comment
Add comment · Show 2 · 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 Tageos · Mar 01, 2016 at 12:14 PM 0
Share

thank you!

avatar image Placus_Brutus Tageos · Mar 02, 2016 at 12:30 AM 0
Share

Hope it helps. If so, please mark it up as the answer. Good luck on your project!

avatar image
2

Answer by Polymo · Feb 29, 2016 at 06:17 PM

 alienScripts = FindObjectsOfType<AlienScript>();

but personally, i would make the manager a Singleton and let the alienscripts register themselves on creation and remove on Destroy.

Comment
Add comment · 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

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

110 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 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

How to make a 2D array of buttons? 2 Answers

How to play a random audio clip from an array in C#? 1 Answer

How to reference UI Panels, in an array for x and y? 0 Answers

Null reference exception in an if statement that checks for it. 1 Answer

Adding an element to an array via script C# 0 Answers


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