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.
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);
}
}
}
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);
}
}
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.
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’
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()));
}
}