Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Assign multiple variables at once

I was wondering if I could assign multiple variables to one object without having to assign it to a variable then assign them. So for example is it possible to convert this:

GameObject obj = Pooler.Instantiate().GetComponent<Asteroid>();
obj.speed = speed;
obj.direction = -1;

To look something like this:

Pooler.Instantiate().GetComponent<Asteroid>().({speed = speed, direction = -1});

When I do that, I get an error, so I was wondering if it was possible to do something like that?

Upvotes: 2

Views: 1153

Answers (1)

DOK
DOK

Reputation: 32831

"Assigning multiple variables at once" would be called object initializers in C#. Here is a classic example:

Old way:

   Person person = new Person();
   person.FirstName = "Scott";
   person.LastName = "Guthrie";
   person.Age = 32; 

New way:

var person = new Person { FirstName="Scott", LastName="Guthrie", Age=32 };

Both syntaxes include the keyword "new".

Upvotes: 4

Related Questions