how to create names array?

I’m creating a game with multiple controllers, when the player press a button it create one more controller.
I want to give the new controller a name (controller) and a number, if the last controller was controller5 the new controller should be controller 6.
If, for example, controller2 diad the new controller name will be controller2.

How can i create a script that do this? The controller prefab called Controller.

Please give me only a direction that i can work with.
I want to work on the script a bit.

Thanks in advance

which language are you working in ? C# or UnityScript ?

C#

anyone know?

Here is what you need. Its only for testing purpose, so you have to modified according your requirement:

using UnityEngine;
using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {

	List<string> nameList=new List <string>{"test2","test3","test4","test1"};
	void Start () {
		var removedNumber=nameList[1].Substring(nameList[1].Length-1);
		print ("removedNumber number is "+removedNumber);
		nameList.RemoveAt(1);
		nameList.Sort();
		var lastNumber=(int.Parse(nameList[nameList.Count-1].Substring(nameList[1].Length-1))+1).ToString();
		print ("lastNumber number is "+lastNumber);
		nameList.Add("test"+removedNumber);
		nameList.Add("test"+lastNumber);
		
		foreach(var i in nameList){
			print (i);
		}
	}
	
}

the player can create 120 controllers max, i need to write all the names?

No you have to dynamically add that controller name to the List.

You mean that I have to write
List nameList=new List {“Controller1”,“Controller2”,“Controller3”,“Controller4”… , “Controller120”};

??

I definitely wouldn’t do it that way. Just make a static class that manages the name - then when a prefab gets destroyed you tell that static class to set it’s next number to that one.

public static class ControlManager
{
    private static int currentControl = 0;
    private static int controlCount = 0;

    private static string prefix = "controller";

    public static string GetCurrentControlName()
    {
        string name = prefix;
        if (currentControl != controlCount)
        {
            name += currentControl;   
        }
        else
        {
            name += controlCount++;
        }
        currentControl = controlCount;
        return name;
    }

    public static void ReportDeath(string name)
    {
        currentControl = int.Parse(name[name.Length - 1]);
    }
}

Then in your prefab - tell the static class if you die

public class Controller : MonoBehaviour
{
    void OnDeath()
    {
        ControlManager.ReportDeath(gameObject.Name);
    }
}

o.k but i need to convert the integer to a string before i do

name += currentControl;
?

Because then it would be (str + str) and not (str + int) , right?

Thank you very much, it is a very good and simple solution…

No - it’ll cast it for you. You may need to do name = name + currentControl (in the event that the + operator is overloaded but the += isn’t) but the effect is the same.

ok thank you again

just one more question

what if more than one controller diad before i create a new controller? the script need to store all the number of the controllers how diad in a array, the problem is that i know how to use arrays only in c and the only language on unity that have the same syntax is JS, someone can help me with that?

and how to fix this problem?

Assets/Controller/namesArray.cs(25,30): error CS1502: The best overloaded method match for `int.Parse(string)’ has some invalid arguments

Assets/Controller/namesArray.cs(25,30): error CS1503: Argument #1' cannot convert char’ expression to type `string’

name is a string what is the problem?

Look up List in MSDN. It’ll let you store an array of arbitrary length.

As to the error - didn’t realize there was no overload for int.Parse.

char numChar = name[name.Length - 1];
currentControl = int.Parse(numChar.ToString());

It works…

I don’t know how to use list and there isn’t information in the scripting refernce site.

How do I create a new list of integers?
How do I add a number?
And how do I delete a number?

MSDN - not the scripting reference. :slight_smile:

http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.90).aspx

ok thank you for your help…

When I call a function GetCurrentControlName that returns me an error

GameObject.Find("Controller(Clone)").name = ControlManager.GetCurrentControlName;

Assets/Controller/controllersNames.cs(14,62): error CS0428: Cannot convert method group GetCurrentControlName' to non-delegate type string’. Consider using parentheses to invoke the method

I changed the script a bit maybe i did something wrong

using UnityEngine;
using System.Collections;
using System.Collections.Generic; 

public static class ControlManager
{
	private static List<int> deadControllers;
    private static int controlCount = 4;

    private static string prefix = "Controller";
	public static string GetCurrentControlName()
    {
        string name = prefix;
		if (deadControllers.Count > 0)
		{
			
	        if (deadControllers[0] != controlCount)
	        {
	            name += deadControllers[0];
				deadControllers.Remove(deadControllers[0]);
	        }
		}
        else
        {
            name += controlCount++;
        }
        return name;
    } 
    public static void ReportDeath(string name)
    {
        char numChar = name[name.Length - 1];
		deadControllers.Add(int.Parse(numChar.ToString()));
    }
}

It’s a method - therefore it needs () at the end.

You might want to consider reading some literature on C# more generally to get a better grasp on the language.

my bad, I just finished a course of three months in c and I made such a foolish mistake XD