2

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?

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • 1
    You can use that syntax as part of object initialization, for example `new Asteroid {speed = 0, direction = 1}`, but only then. Seems like it's not possible in your case. – SimpleVar Feb 14 '15 at 17:57

1 Answers1

4

"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".

DOK
  • 32,337
  • 7
  • 60
  • 92