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 youngapprentice · Feb 11, 2014 at 05:50 PM · arraylistgetcomponentclassclass instance

Array of Objects of a Class

I would assume this is relatively straightforward. I just switched over from UnityScript to C#, and I have to admit it is largely superior, even in the fact that it very tangibly helps you understand the object-class relationships in the game project. I all ready prefer it.

Anyways, I'm just trying to get a quick array set up of references to instances of a SpawnPoint class. SpawnPoint is a script attached to a Spawn Point prefab, and I'm just cycling through them when a level loads to get a list of them.

However, I am having trouble populating this array.

To get a reference to the class, I am using GetComponent. I understand why this doesn't work- GetComponent returns a type and not a variable/value. I just don't know what other method I should employ here.

Thanks for the help! - YA

P.S: Is there any way for me to get rid of the automagically created 'Use this for initialization' and 'Updates every frame' comments? They're a small annoyance but still...

 using UnityEngine;
 using System.Collections;
 
 public class SpawnCont : MonoBehaviour {
     
     public SpawnPoint[] pointArr;
     public GameObject player;
     public SpawnPoint current;
 
     // Use this for initialization
     void Start () {
         
         pointArr = new SpawnPoint[ transform.childCount ];
         //Because of this, don't make anything but spawnpoints children
         
         int count = 0;
         //Make sure we get through full list
 
         foreach( Transform child in transform ){
 
             //The important part 
             //THIS IS WHERE THE ERROR IS
             pointArr[count] = child.GetComponent( SpawnPoint );
             count += 1;
         }
 
         SetActive ( pointArr[0] );
         Debug.Log ( "Spawning Player at: " + pointArr[0].pos.x );
         Spawn( player );
     }
     
     void SetActive( SpawnPoint myPoint ){
         for( int i = 0; i < pointArr.Length; i++ ){
             pointArr[i].activated = false;
         }
         myPoint.activated = true;
         current = myPoint;
     }
 
     void Spawn( GameObject player ){
         Instantiate( player, current.pos, Quaternion.identity );
     }
 }
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 iamvishnusankar · Feb 11, 2014 at 06:11 PM

1.If you're trying to get the SpawinPoint from SpawnPointScript

2.First define a spawn point return function on your SpawnPoint script.

2.1.For example let the function be "ReturnSpawnPoint()"

  1. And at the start() of SpawnCount script use the following code:

using UnityEngine; using System.Collections;

public class SpawnCont : MonoBehaviour {

 public SpawnPoint[] pointArr;
 public GameObject player;
 public SpawnPoint current;
 
 // Use this for initialization
 void Start () {
 
    pointArr = new SpawnPoint[ transform.childCount ];
    //Because of this, don't make anything but spawnpoints children
 
    int count = 0;
    //Make sure we get through full list

    //this will create an instance 
      current = child.GetComponent<SpawnPoint>();

 
    foreach( Transform child in transform ){
 
      //The important part 

    

     //This will assign the spawnpoint

      pointArr[count] = current.ReturnSpawnPoint();
      count += 1;
    }
 
    SetActive ( pointArr[0] );
    Debug.Log ( "Spawning Player at: " + pointArr[0].pos.x );
    Spawn( player );
 }

Comment
Add comment · Show 3 · 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 youngapprentice · Feb 11, 2014 at 06:33 PM 0
Share

$$anonymous$$y Return function was a very simple:

 public SpawnPoint ReturnSpawnPoint(){
         return this;
     }

And your solution worked well. However, is there any other way? I would hate to waste my time writing otherwise useless self-return functions every time I want a reference to an instance of a class on an Object. I refuse to believe there are no other solutions.

Thanks for the help! - YA

avatar image iamvishnusankar · Feb 11, 2014 at 06:40 PM 0
Share

Yes, Ins$$anonymous$$d of writing repeated self returning functions on every object you want to get spawned or something like that ;

Just make one "Empty GameObject" and and attach all your "$$anonymous$$aster Scripts". Use that Object as the reference for all other objects. Thereby you could solve the problem of repeat blocks of code.

avatar image youngapprentice · Feb 11, 2014 at 07:55 PM 0
Share

Haha that is even more impractical than before. This is fine it will work for now. Thank you once again! -YA

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

19 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

Related Questions

A node in a childnode? 1 Answer

how would I make an array of different scripts 1 Answer

Adding a class with Generic Lists inside a Generic List 1 Answer

Accesing custom class object from another script 1 Answer

Level generation using a "tree" type structure, why when i add a cell i get an error argument out of range? 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