none
How can i pass array from one class file to another class file ? RRS feed

  • Question

  • In the first file i have the code:

    using UnityEngine;
    using Random = UnityEngine.Random;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    
    public class CloneSpaceShips : MonoBehaviour {
    
        public GameObject Spaceship;
        private GameObject[] spaceships;
    
        // for tracking properties change
        private Vector3 _extents;
        private int _spaceshipCount;
        private float _spaceshipSize;
    
        /// <summary>
        ///     How far to place spheres randomly.
        /// </summary>
        public Vector3 Extents;
    
        /// <summary>
        ///     How many spheres wanted.
        /// </summary>
        public int SpaceShipCount;
        public float SpaceShipSize;
    
        // Use this for initialization
        void Start () {
    
            Clone();
            spaceships = GameObject.FindGameObjectsWithTag("SpaceShip");
        }


    I want to pass the spaceships array variable.

    I can make it public static but i don't want to do it this way.

    The other way is to use get; set; so i tried:

    public GameObject[] Spaceships
    	{
    		get
    		{
    			return spaceships;
    		}
    		set
    		{
    			spaceships = value;
    		}
    	}

    And then in the second class file i did:

    CloneSpaceShips css = new CloneSpaceShips;
    arrayinsecondclassfile = css.Spaceships;

    But the arrayinsecondclassfile is always null.

    Monday, October 17, 2016 11:41 PM

Answers

  • Hi Chocolade1972,

    Thank you for posting here.

    For your question, you want to pass array from one class file to another class file. You have no value for Spaceships.

    According to your code, some variables defined by yourself but did not show in the code.

    I make a simple example to show how to pass array from one class file to another class file with get method.

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace pass_array_from_one_class_file_to_another_class_file_2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 C1 = new Class1();
                string[] test = C1.array;
    
                foreach (var item in test)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
        }
    }

    Class1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace pass_array_from_one_class_file_to_another_class_file_2
    {
        class Class1
        {
            //private GameObject[] spaceships;
            private string[] _array = new string[] { "a", "b", "c", "d" };
            public string[] array
            {
                get { return this._array; }
                set { this._array = value;}             
            }  
        }
    }

    Here is the output.

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, October 18, 2016 7:16 AM
    Moderator

All replies

  • You don't need a private backing property on a get/set.

    https://msdn.microsoft.com/en-us/library/bb384054.aspx?f=255&MSPPError=-2147217396

    And how do you know it found anything?

    spaceships = GameObject.FindGameObjectsWithTag("SpaceShip");

    If it didn't find anything, the value would be null I would think, unless this is some method you made.

    Tuesday, October 18, 2016 12:45 AM
  • Hi Chocolade1972,

    Thank you for posting here.

    For your question, you want to pass array from one class file to another class file. You have no value for Spaceships.

    According to your code, some variables defined by yourself but did not show in the code.

    I make a simple example to show how to pass array from one class file to another class file with get method.

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace pass_array_from_one_class_file_to_another_class_file_2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 C1 = new Class1();
                string[] test = C1.array;
    
                foreach (var item in test)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
        }
    }

    Class1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace pass_array_from_one_class_file_to_another_class_file_2
    {
        class Class1
        {
            //private GameObject[] spaceships;
            private string[] _array = new string[] { "a", "b", "c", "d" };
            public string[] array
            {
                get { return this._array; }
                set { this._array = value;}             
            }  
        }
    }

    Here is the output.

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, October 18, 2016 7:16 AM
    Moderator
  • First off, this is Unity development. While Unity does use a Dialect of C# it targets the Mono Framework. So a Mono or Unity related forum would be the better place to ask questions. Not even Random behaves the same way in Mono as it does in .NET.

    For General game design you might also ask on the XNA Subforum, but again a Unity Forum would be better (as they know wich approaches actually work on Unity).

    How to share this data depends when and how these two classes (in those two seperate files) will be instantiated. And by whom. A list of game objects must be persistet between Updates and Draws either way. Once you have the list, you can just hand it in to wichever code needs it.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Tuesday, October 18, 2016 10:33 AM