- Home /
I can do this in JS or not ???
Hello
I can do this in JS or not ???
plz do not tell me learn JS >>> only give me the answer .. I can or not >>> if I can >>> how to do it >> if doesn't >>> that mean the JS is not powerful like C# >>> is this right
.................................................. I try many times and give me error
'Add' is not a member of 'UnityEngine.GameObject[]'. or 'Add' is not a member of 'UnityEngine.Transform[]'. ...........................................................
Thx>>>> anyway..
It's true that there are some language specific things you can do in C# that JS doesn't support, but for a beginner, you don't need to worry about them. They won't be a hindrance to you to begin with anyway.
The reason I'm commenting and not answering is that I'm not sure what kind of answer you really expect. If you want to know whether you can target enemies in JS, then the answer is yes. As long as you're a beginner to program$$anonymous$$g, consider the capabilities of all 3 unity supported languages completely equal. What you can accomplish in one, you can accomplish in another as well.
When I started out, I had the choice between JS and C#. I am endlessly grateful that I chose C#.
I concur. But this question is not about personal language preference. It is about what can be accomplished in them.
I think I misunderstood something in the question, there. I thought that when the OP said "plz do not tell me learn JS" that they knew C# and didn't want to learn JavaScript. I think, that's not it- I think that what they actually meant, was "please, I don't want to learn any program$$anonymous$$g languages, I just want to have the answers handed to me on a plate". Yes, upon rereading, that's definitely what they said.
In which case, my answer is useless to them, in every possible way.
Sorry.
In any case, I should totally learn Boo just so that I can answer questions like this in it.
Answer by Graham-Dunnett · Oct 21, 2011 at 12:43 PM
I have only quickly tested this, but I think it is what you need. If you run into this kind of problem again, it's far quicker for you to post the code that you do have and does not work. To answer you I had to follow along the tutorial and write JS whilst the dude was writing c# which took me around 7 minutes. Possibly I could have corrected your code in 30 seconds.
import System.Collections.Generic;
var targets : List.<Transform>;
function Start () {
targets = new List.<Transform>();
AddAllEnemies();
}
function Update () {
}
function AddTarget( t : Transform) {
targets.Add(t);
}
function AddAllEnemies() {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in gos) {
AddTarget(go.transform);
}
}
now I can from this script understand more about JS.. >>>>
thx to anyone help me or try to help
I was thought that the JS does not need for Import system like C#..
Answer by syclamoth · Oct 21, 2011 at 12:41 PM
In any case, the specific problem you are trying to solve here, is that arrays in C# are always static, and cannot change their sizes. You should be using generic lists, instead!
// You need to put this line at the top of the file
// near the UnityEngine and System.Collections bits-
using System.Collections.Generic;
then, when you would define the array-
// instead of Transform[] name = new Transform[length];
List<Transform> transforms = new List<Transform>();
Then, when you want to add a member to it,
transforms.Add(newTransform);
You can iterate through that like this-
foreach(Transform trans in transforms)
{
// do some operation on trans
Debug.Log(trans.position);
}
You can delete specific transforms out of the list if you need to-
transforms.Remove(someTransform);
this will remove that transform from the list.
Remember, the 'Generic' in generic list means you can do this for any type! Just replace Transform in the type declaration with whatever type you need.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Script not working (Java script) 2 Answers
Play music trough scenes 0 Answers
Fonts not displaying properly when loaded through script 0 Answers