Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ineteye · Oct 11, 2012 at 02:29 PM · c#

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ineteye · Oct 11, 2012 at 04:24 PM 0
Share

Big thanks ))))))))!!!!!

avatar image kmeboe · Oct 11, 2012 at 04:25 PM 0
Share

You are most welcome! Don't forget to mark the question as answered.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges