0

In MainClass I have:

public List<TransformData> matrices1 = new List<TransformData>(); public Vector3 scaleVector = 1f; 

In TransformData class I have:

public class TransformData { public Vector3 position; public Quaternion rotation; public Vector3 scale; } 

Back in MainClass, I want to add the info from TransformData into the variable matrices1. How do you do this right? I have-

matrices1.Add(TransformData.(TransformData.position, TransformData.rotation, scaleVector)); 

That gives me errors. I saw other StackOverflow questions about this but i cant get it right

1
  • Welcome to StackOverflow! Always if you get some exceptions or output in the console you didn't expect it would help if you added it as text to your question. Commented Jun 19, 2019 at 6:44

3 Answers 3

2

Here you go sir:

TransformData td = new TransformData(); matrices1.add(td); 

If you would like to add parameters right at the declearation, add a constructor to your TransformData class.

public class TransformData { public Vector3 position; public Quaternion rotation; public Vector3 scale; public TransformData(){} public TransformData(Vector3 pos, Quaternion rot, Vector3 sc) { position = pos; rotation = rot; scale = sc; } } 

and then you can do it like u did in you code.

matrices1.add(new Transformdata(somepos, somerotation, somevector)); 
Sign up to request clarification or add additional context in comments.

Comments

1

You should give it a constructor like

// In order to be able to actually save that list this class has to be // Serializable! // Another nice side effect is that from now you can also adjust the values // from the Inspector of the MonoBehaviour using such an instance or list [Serializable] public class TransformData { public Vector3 position; public Quaternion rotation; public Vector3 scale; // Serialization always needs a default constructor // doesn't have to do anything but can public TransformData() { // scale should be 1,1,1 by default // The other two can keep their default values scale = Vector3.one; } public TransformData(Vector3 pos, Quaternion rot, Vector3 scal) { position = pos; rotation = rot; scale = scal; } } 

and then do e.g.

// this is a vector not a float! public Vector3 scaleVector = Vector3.one; private void Start() { // I guess you wanted to add the data for some transform component matrices1.Add(new TransformData(transform.position, transform.rotation, Vector3.one)); } 

Note that Add can not be used outside of a method.


If you rather want to directly initialize your list with some elements added you could go like

public List<TransformData> matrices1 = new List<TransformData>() { new TransformData(somePosition, someRotation, scaleVector); }; 

if somePosition and someRotation are e.g. also values you get from the Inspector. You can't use transform.position etc outside of a method.

2 Comments

@kame it's the same, yes ;) For Attributes you can remove the () if there are no parameters
Thank you for the well-commented explanation. I finally got my project working with your and everybody's help.
1

You need to add variable or reference to object that holds the data:

TransformData myObj = new TransformData(); myObj.position = //populate it here myObj.rotation = //populate it here myObj.scale = //populate it here 

and then add it to the list:

matrices1.Add(myObj); 

If you have that object somewhere else already created, then just add the variable that holds it.

What you've seen on the Stack Overflow that looks similar to line posted in question is probably that:

matrices1.Add(new TransformData(){ position = //something, rotation = //something, scale = //something }); 

Which is creating new object as well as adding it to a list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.