#How to add a new value at the end of an array (like pushback in c++)

1 messages · Page 1 of 1 (latest)

noble echo
#

Hello i have been looking to add a new value at the end of an array of transforms

#

void SpawnPoint()
{
var New = Instantiate(point,worldposition, Quaternion.identity, startingpoint);
lr.points.SetValue(New,lr.points.Length);

}
#

this is the code i have been using for spawning new points in my game

#

and lr.points is a transform[]

#

currently the set value isnt working

#

if anyone could help

forest glen
#

not sure if this is lists or arrays but have you tried the Append function?

#

Add*

noble echo
oblique crane
#

Because an array itself can not be extended. If you change it to a list, you can use the add function

noble echo
#

uhh

#

how do i do that?

#

it wouldn't work

#

becouse when i do

#

one of my functions flip up

mental bridge
#

I mean, if you change the type of a variable, you'll have to change the type of the functions you pass that variables to as well.
So basically you have a few options

  • change parameter of the method to the type of the variable
  • change parameter of the method to a type the variable implements (don't do this if you don't understand inheritance and polymorphism)
  • change variable to an array while passing using ToArray