1

Here's my Class:

public class Mark {

    public GameObject InstanciatedObject;
    public Vector3 ControllerPosition;
    public Quaternion ControllerRotation;

    public void CreateMark(Mark m, GameObject o, Vector3 p, Quaternion r)
    {
        m.InstanciatedObject= o;
        m.ControllerPosition= p;
        m.ControllerRotation = r;
    }
}

And I want to use the following lines in another script:

Mark m = new Mark();
m.CreateMark(m, ControllerObject, GetControllerPosition(), GetControllerRotation());

The problem here is that I don't want to create the name of the object manually. I want to create, for instance, m1, m2, m3, etc.

1

2 Answers 2

1

You cannot create variable names at runtime, if that is possible that would not be a basic situation suitable for your level.

Best would be to consider an array:

MarksArray [] m = new MarksArray[size];

for(int i = 0; i < size ; i ++){
    m[i] = new Mark();
    m[i].CreateMark(param1, param2,...);
}

then you can use the variable like this:

m[0].member = value;

As a side note, you should start complying with C# coding convention, classes and methods use Cap letter on the front.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that will do. Thanks for the quick reply. And yes, I will keep in mind the coding convention from now on. I actually create my own conventions so that my code doesn't get too messy, but I do understand that is better to follow the general guidelines so it is also easy for other programmers to understand.
Arrays are good if you know the size ahead of time, for a game there would probably be quite a few cases where you'd need a dynamic list though
The coding convention are pretty important since Unity provides a certain convention. That would clearly separate your code from Unity code but I would not recommend it. And you will probably work with others one day, they won't like it and then they won't like you. As for the array/list conflict, you can still jump from one to the other if needed. Not the best but still possible.
Hi @mr.martins. you MUST follow coding "conventions". it's just that simple. please edit your question to conform to coding conventions for the language.
0

If are you doing this so you can have something such as a list of multiple unique objects of the same type, such as multiple enemies on the screen you may be better served by using the following:

public class Mark {

public GameObject InstantiatedGameObject;
public Vector3 ControllerPosition;
public Quaternion ControllerRotation;

public Mark(GameObject o, Vector3 p, Quaternion r)
{
    InstantiatedGameObject = o;
    ControllerPosition = p;
    ControllerRotation = r;
}

Then, you would use the following code:

using System.Collections.Generic;

public class Main {

   public List<Mark> MarkList = new List<Mark>()

   public void Main() {
      // do stuff to get the needed variables
      MarkList.Add(new Mark(ControllerObject, GetControllerPosition(), GetControllerRotation()));
   }
}

This will keep adding new, unique instances of the aMark class objects into an easily accessible list. The benefit to using this method over using an array is that you don't need to determine the maximum size ahead of time, you can dynamically add and remove objects as needed.

3 Comments

I tried that and when I declare the List it says: The type or namespace name `List<aMark>' could not be found. Are you missing a using directive or an assembly reference?
Ah sorry, I forgot the using statement. I have updated my example
Thank you. I'll stick to that solution because I want to instantiate an undefined number of Objects and acess it's properties. In my case, the List suits better than the arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.