- Home /
this script in Javascript??
hello!
i am trying to recreate a C# script into a Javascript but i can seem to get it. here is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Target : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
AddAllEnemies();
selectedTarget = null;
myTransform = transform;
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add (enemy);
}
private void SortTargetsByDistance()
{
targets.Sort (delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count -1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget()
{
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PLayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
If your primary language is javascript, you can still use this script by placing it into the Plugins folder. I doubt anyone here will be willing to convert the script for you. There are some free online converters(they can be spotty), or you can post your javascript version and where you are stuck.
well, your problem is it's still in C#. try converting it to javascript and maybe someone can help you if you have an issue
There really isnt much to convert anyway, here are the basics
functions
public void $$anonymous$$yFunction()
to
function $$anonymous$$yFunction()
Lists
List<Component>
to
List.<Component>
variables
GameObject[] gos
to
var gos:GameObject[]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with conversion from javascript to c# 3 Answers
Can someone help me translate this to c#? 1 Answer
java to C# 2 Answers
Convert to array. 1 Answer