- Home /
This post has been wikified, any user with enough reputation can edit it.
Need help converting js to C
Hi!
original js script:
#pragma strict
#pragma implicit
#pragma downcast
//this script is attached every ball and it must be disabled, clickdetecting script will enable this
var particle:GameObject;
private var count:boolean=true;
function Start()
{
var container = GameObject.Find("Container"); //container which contains ball's objects
rigidbody.position = Vector3(rigidbody.position.x,rigidbody.position.y, 0);
var myPos : Vector3 = transform.position;
for (var child : Transform in container.transform) //check if there is same tagged ball near this ball
{
var t : Transform = child.transform;
t.position.z=0;
myPos.z=0;
distance = (t.position - myPos).sqrMagnitude;
if (distance>0.0001 && distance <0.1 && gameObject.tag==child.gameObject.tag) //if distance is less then 0.1 between same balls
{
child.gameObject.GetComponent.<chack>().enabled=true; //enable same ball's "chack" script
var modifiedColors : Color[] = particle.GetComponent.<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
modifiedColors[0] = renderer.material.color;
modifiedColors[1] = renderer.material.color;
modifiedColors[2] = renderer.material.color;
modifiedColors[3] = renderer.material.color;
modifiedColors[4] = renderer.material.color;
particle.GetComponent.<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
var particle=Instantiate(particle, transform.position, transform.rotation); //instantiate particle
if(count){
clickdetecting.destroyed++;
if(Time.timeScale != 0){
Camera.main.GetComponent.<clickdetecting>().scoresCalculation();//call clickdetecting script which is attached to main camera
}
count=false;
}
Destroy(gameObject);
}
}
}
Converted to C# ... fixed what i can:
using UnityEngine;
using System.Collections;
public class chack : MonoBehaviour {
//this script is attached every ball and it must be disabled, clickdetecting script will enable this
GameObject particle;
private bool count=true;
void Start (){
GameObject container= GameObject.Find("Container"); //container which contains ball's objects
rigidbody.position = new Vector3(rigidbody.position.x,rigidbody.position.y, 0);
Vector3 myPos = transform.position;
foreach(Transform child in container.transform) //check if there is same tagged ball near this ball
{
Transform t = child.transform;
t.position = new Vector3(rigidbody.position.x,rigidbody.position.y, 0);
//myPos.z=0;
myPos = new Vector3(myPos.x,myPos.y, 0);
//
distance = (t.position - myPos).sqrMagnitude;
//error CS0103: The name `distance' does not exist in the current context
//
if (distance>0.0001f && distance <0.1f && gameObject.tag==child.gameObject.tag) //if distance is less then 0.1f between same balls
{
//
child.gameObject.GetComponent(chack).enabled=true; //enable same ball's "chack" script
//error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
//error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments
//error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'
//
Color[] modifiedColors = particle.GetComponent(ParticleAnimator).colorAnimation; //get particle color and modify it to match this ball's color
modifiedColors[0] = renderer.material.color;
modifiedColors[1] = renderer.material.color;
modifiedColors[2] = renderer.material.color;
modifiedColors[3] = renderer.material.color;
modifiedColors[4] = renderer.material.color;
particle.GetComponent(ParticleAnimator).colorAnimation = modifiedColors; //give particle modified color
//
var particle=Instantiate(particle, transform.position, transform.rotation); //instantiate particle
//error CS0136: A local variable named `particle' cannot be declared in this scope because it would give a different meaning to `particle', which is already used in a `parent or current' scope to denote something else
//error CS0841: A local variable `particle' cannot be used before it is declared
//error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments
//error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Object'
//
if(count){
//
clickdetecting.destroyed++;
//error CS0122: `clickdetecting.destroyed' is inaccessible due to its protection level
//
//
if(Time.timeScale != 0){
//
Camera.main.GetComponent(clickdetecting).scoresCalculation();//call clickdetecting script which is attached to main camera
//error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
//error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments
//error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'
//
}
count=false;
}
Destroy(gameObject);
}
}
}
}
Comment
Best Answer
Answer by kmeboe · Oct 11, 2012 at 04:01 PM
Here you go:
using UnityEngine;
using System.Collections;
public class chack : MonoBehaviour
{
//this script is attached every ball and it must be disabled, clickdetecting script will enable this
GameObject particle;
private bool count = true;
void Start ()
{
GameObject container = GameObject.Find ("Container"); //container which contains ball's objects
rigidbody.position = new Vector3 (rigidbody.position.x, rigidbody.position.y, 0);
Vector3 myPos = transform.position;
foreach (Transform child in container.transform) { //check if there is same tagged ball near this ball
Transform t = child.transform;
t.position = new Vector3 (rigidbody.position.x, rigidbody.position.y, 0);
//myPos.z=0;
myPos = new Vector3 (myPos.x, myPos.y, 0);
//
var distance = (t.position - myPos).sqrMagnitude;
if (distance > 0.0001f && distance < 0.1f && gameObject.tag == child.gameObject.tag) { //if distance is less then 0.1f between same balls
//
child.gameObject.GetComponent<chack> ().enabled = true; //enable same ball's "chack" script
Color[] modifiedColors = particle.GetComponent<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
modifiedColors [0] = renderer.material.color;
modifiedColors [1] = renderer.material.color;
modifiedColors [2] = renderer.material.color;
modifiedColors [3] = renderer.material.color;
modifiedColors [4] = renderer.material.color;
particle.GetComponent<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
//
var newParticle = Instantiate (particle, transform.position, transform.rotation); //instantiate particle
if (count) {
//
clickdetecting.destroyed++;
if (Time.timeScale != 0) {
//
Camera.main.GetComponent<clickdetecting>().scoresCalculation ();//call clickdetecting script which is attached to main camera
}
count = false;
}
Destroy (gameObject);
}
}
}
}
Since we don't have access to the "clickdetecting" script, I can't fix the "inaccessible" error (for "clickdetecting.destroyed++;"). What probably needs to happen is that "destroyed" needs to be made public in the clickdetecting script, like this:
public int clickdetecting;
You are most welcome! Don't forget to mark the question as answered.
Your answer
Follow this Question
Related Questions
Need help converting js to C# - rigidbody.rotation.z 1 Answer
Need help C# -- yield, destroy 1 Answer
Enemy AI script causing Unity to crash (Javascript to C#) 1 Answer
Need help converting js to C 2 Answers
Multiple Cars not working 1 Answer