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 lexen1 · Apr 04, 2014 at 05:20 PM · c#arrayvariablespawn

C# Make array based on script variable

Thank you for reading my question. I have a number of objects that have a script on them called "SpawnPoint" that have a few variables including "Team ID." I am trying to make an array of only objects that both have the Spawn Point script attached, and have the Team ID variable set to 0.

I can make an array of all Spawn Points by:

 SpawnPoint[] spawnPoints;
 
 void Start () {
 spawnPoints = GameObject.FindObjectsOfType <SpawnPoint>();
 }

But that is as far as I can get. Any help would be greatly appreciated.

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
1

Answer by Uraani · Apr 04, 2014 at 06:15 PM

This is just how i'd do it:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SpawnPoint : MonoBehaviour 
 {
     //static list for the spawnPoints
     public static List<SpawnPoint> spawnPoints = new List<SpawnPoint> ();
     //assuming its an integer
     public int teamID;
     // Use this for initialization
     void Start () 
     {
         spawnPoints.Add (this);
     }
     void OnDestroy() 
     {
         spawnPoints.Remove (this);
     }
     public static SpawnPoint[] GetTeamIDSpawns(int ID)
     {
         List<SpawnPoint> teamPoints = new List<SpawnPoint> ();
         foreach(SpawnPoint spawnPoint in spawnPoints)
         {
             if(spawnPoint.teamID == ID)
             {
                 teamPoints.Add(spawnPoint);
             }
         }
         return teamPoints.ToArray ();
     }
 }

 

pass the searched ID value in the parameters in this case its 0

How to use it:

 //this gets all SpawnPoints with teamID of 0
 spawnPoints = SpawnPoint.GetTeamIDSpawns(0);
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 lexen1 · Apr 04, 2014 at 09:07 PM 0
Share

Thanks man! That did the trick. Is there a way to check two variables? I have a bool called "isSeen" where if a character is looking at a spawn point, isSeen becomes true. I know, I'm asking a lot. Thanks either way!

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

21 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

Related Questions

[SOLVED]Object selection scrip doesn't work. 2 Answers

Toggle Control between multiple turrets 0 Answers

Spawning the character with camera behind it on load using c# 1 Answer

Inspector Definitions for New Script Components Problem 0 Answers

How to call Variable across scripts. 2 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