Meaning of the code written below in c# unity scripting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Meaning of the code written below in c# unity scripting

I'm creating a tower defens game via watching tutorials but couldn't understand the meaning of code or more precisely why we used two transform words and what exactly are we storing in points array variable the code is " points = new Transform[transform.childCount];"

15th Jun 2018, 6:07 PM
Khushboo Gupta
Khushboo Gupta - avatar
8 Answers
+ 4
By the way, this is the wrong site to ask this type of question; you're lucky to have even gotten an answer. In the future: https://unity3d.com/community ^Go there. They have a community of people dedicated to assisting with Unity and a very extensive database of information that would also assist you. Best of luck!
15th Jun 2018, 6:18 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
It's creating a series of waypoints. transform.childCount is counting how many children are part of that object, and then it's using that number to set the size of our array 'points.' Afterwards, you use a loop and assign each child to each point in the array 'points.' Hope that helps.
15th Jun 2018, 6:15 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Basically, what the code does is creates an array called points that stores variables of type Transform. The ammount of elements the array has is equal to the amount of children the object that the script is sitting on has. Now I will explain what the deal with 2 transforms is. Transform with big T means the type transform (the component every game object has). transform with lower t means the transform that is sitting on the object that has the script. I know there were a couple of twists in my explanation, but I recommend you read it a couple of times to understand it.
15th Jun 2018, 6:17 PM
Alexandru Turculet
Alexandru Turculet - avatar
+ 2
transform.childCount is the way you have to do it, no shortcut unfortunately
15th Jun 2018, 6:29 PM
Alexandru Turculet
Alexandru Turculet - avatar
+ 2
@Khushboo Gupta As Androidus explained, Transform[] (one with capital T) is the class and the one with a lowercase T is the actual object being instantiated in your world. So you use transform.childCount because you're referencing the instance that exists in the world, and then you're count its children. 'childCount' is just the method of that class which counts the children. Also, the array 'points' is going to be used to reference the location of those child objects. That's why we used the childCount to create the size of our array, as we'll be storing each child's location into this array to use as our waypoints. (that's why they named the variable points) Okay, I understand then about using the other site's forum. Another website that I recommend you bookmark (for most things) is www.StackOverflow.com. It's a priceless resource and you can get really good assistance from people all over there. Just be advised that they have strict rules you'll want to learn prior, but once you learn them and get familiar with their system, it's an amazing resource for asking questions.
15th Jun 2018, 7:37 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Fata1 Err0r Androidus 3 then why have we used second transform since we are not concerned with the location of child objects so can't we write the code like points = new Transform [childCount] or something like this...I'm new to this so not much familiar with the syntax And I have tried that unity site too but it takes a lot of time to get any reply whereas Sololearn gives in minutes only,so I posted here And thanks for giving your time to my question 😊
15th Jun 2018, 6:27 PM
Khushboo Gupta
Khushboo Gupta - avatar
+ 1
Thank you so much,all my doubts got cleared. And I'll certainly have a look at that website too,
16th Jun 2018, 5:32 AM
Khushboo Gupta
Khushboo Gupta - avatar
0
okay,thanks
15th Jun 2018, 6:33 PM
Khushboo Gupta
Khushboo Gupta - avatar