Search Unity

Help with Script,converting Java to C#

Discussion in 'Scripting' started by Berir, Mar 18, 2015.

  1. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    // Can anyone please tell me how do I write this in c# ...thanks a lot


    public void FireProjectile()
    {

    var newMissile = Instantiate(myProjectile, muzzlePositions.position, muzzlePositions.rotation);
    newMissile.GetComponent(Projectile_Missile).myTarget = myTarget;
    }
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    It's hard to see what your classes consists of, but that code will compile in C#, newMissile is just typed implicitly.

    If you're having errors post them

    edit: you might need to change this line:
    Code (csharp):
    1. newMissile.GetComponent(Projectile_Missile).myTarget = myTarget;
    to
    Code (csharp):
    1. newMissile.GetComponent("Project_Missile").myTarget = myTarget;
    or
    Code (csharp):
    1. newMissile.GetComponent<Project_Missile>().myTarget = myTarget;
     
  3. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    thank you for your reply it is a script for a turret that shot missile ....

    with your first suggestion I get this error

    error CS1061: Type `UnityEngine.Object' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)
     
  4. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    2nd suggestion gives me this error

    error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
     
  5. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    This is original script of turret in js (tower defence) , I am trying to change it into c# but those 2 lines above are giving me lots of problems ... thanks for any help in advance



    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var myProjectile : GameObject;
    4. var reloadTime : float = 1f;
    5. var turnSpeed : float = 5f;
    6. var firePauseTime : float = .25f;
    7. var errorAmount : float = .001;
    8. var myTarget : Transform;
    9. var muzzlePositions : Transform[];
    10. var pivot_Tilt : Transform;
    11. var pivot_Pan : Transform;
    12. var aim_Pan : Transform;
    13. var aim_Tilt : Transform;
    14.  
    15. private var nextFireTime : float;
    16.  
    17. function Start ()
    18. {
    19.  
    20. }
    21.  
    22. function Update ()
    23. {
    24.     if(myTarget)
    25.     {
    26.         aim_Pan.LookAt(myTarget);
    27.         aim_Pan.eulerAngles = Vector3(0, aim_Pan.eulerAngles.y, 0);
    28.         aim_Tilt.LookAt(myTarget);
    29.      
    30.         pivot_Pan.rotation = Quaternion.Lerp(pivot_Pan.rotation, aim_Pan.rotation, Time.deltaTime*turnSpeed);
    31.         pivot_Tilt.rotation = Quaternion.Lerp(pivot_Tilt.rotation, aim_Tilt.rotation, Time.deltaTime*turnSpeed);
    32.      
    33.         if(Time.time >= nextFireTime)
    34.         {
    35.             FireProjectile();
    36.         }
    37.     }
    38. }
    39.  
    40. /*
    41. function OnTriggerEnter(other : Collider)
    42. {
    43.     if(other.gameObject.tag == "Enemy")
    44.     {
    45.         nextFireTime = Time.time+(reloadTime*.5);
    46.         myTarget = other.gameObject.transform;
    47.     }
    48. }
    49. */
    50.  
    51. function OnTriggerStay(other : Collider)
    52. {
    53.     if(!myTarget)//if I don't already have a target
    54.     {
    55.         if(other.gameObject.tag == "Air Enemy")
    56.         {
    57.             nextFireTime = Time.time+(reloadTime*.5);
    58.             myTarget = other.gameObject.transform;
    59.         }
    60.     }
    61. }
    62.  
    63. function OnTriggerExit(other : Collider)
    64. {
    65.     if(other.gameObject.transform == myTarget)
    66.     {
    67.         myTarget = null;
    68.     }
    69. }
    70.  
    71. function FireProjectile()
    72. {
    73.     audio.Play();
    74.     nextFireTime = Time.time+reloadTime;
    75.  
    76.     var m : int = Random.Range(0,6);
    77.     var newMissile = Instantiate(myProjectile, muzzlePositions[m].position, muzzlePositions[m].rotation);
    78.     newMissile.GetComponent(Projectile_Missile).myTarget = myTarget;
    79. }
    80.  
     
  6. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
     
  7. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    //This is what I ve got so far in C#


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ConvertedSam : MonoBehaviour {
    5.    
    6.    
    7.     public GameObject myProjectile;
    8.     public float reloadTime = 1f;
    9.     public float turnSpeed = 5f;
    10.     public float firePauseTime = .25f;
    11.     public float errorAmount = .001f;
    12.     public Transform myTarget;
    13.     public Transform[] muzzlePositions;
    14.     public Transform pivot_Tilt;
    15.     public Transform pivot_Pan;
    16.     public Transform aim_Pan;
    17.     public Transform aim_Tilt;
    18.    
    19.     private float nextFireTime;
    20.    
    21.     public void Start ()
    22.     {
    23.        
    24.     }
    25.    
    26.     public void Update ()
    27.     {
    28.         if(myTarget)
    29.         {
    30.             aim_Pan.LookAt(myTarget);
    31.             aim_Pan.eulerAngles = new Vector3(0, aim_Pan.eulerAngles.y, 0);
    32.             aim_Tilt.LookAt(myTarget);
    33.            
    34.             pivot_Pan.rotation = Quaternion.Lerp(pivot_Pan.rotation, aim_Pan.rotation, Time.deltaTime*turnSpeed);
    35.             pivot_Tilt.rotation = Quaternion.Lerp(pivot_Tilt.rotation, aim_Tilt.rotation, Time.deltaTime*turnSpeed);
    36.            
    37.             if(Time.time >= nextFireTime)
    38.             {
    39.             FireProjectile();
    40.             }
    41.         }
    42.     }
    43.    
    44.     /*
    45.     function OnTriggerEnter(other : Collider)
    46.     {
    47.         if(other.gameObject.tag == "Enemy")
    48.         {
    49.             nextFireTime = Time.time+(reloadTime*.5);
    50.             myTarget = other.gameObject.transform;
    51.         }
    52.     }
    53.     */
    54.    
    55.     public void OnTriggerStay(Collider other)
    56.     {
    57.         if(!myTarget)//if I don't already have a target
    58.         {
    59.             if(other.gameObject.tag == "Air Enemy")
    60.             {
    61.                 nextFireTime = Time.time+(reloadTime*.5f);
    62.                 myTarget = other.gameObject.transform;
    63.             }
    64.         }
    65.     }
    66.    
    67.     public void OnTriggerExit(Collider other)
    68.     {
    69.         if(other.gameObject.transform == myTarget)
    70.         {
    71.             myTarget = null;
    72.         }
    73.     }
    74.    
    75.     public void FireProjectile()
    76.     {
    77. //        audio.Play();
    78. //        nextFireTime = Time.time+reloadTime;
    79. //      
    80. //        int m = Random.new Range(0,6);
    81. //        var newMissile = Instantiate(myProjectile, muzzlePositions[m].position, muzzlePositions[m].rotation);
    82. //        newMissile.GetComponent(Projectile_Missile).myTarget = myTarget;
    83.     }
    84. }
    85.  
    86.  
     
  8. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    Change var newMissile to GameObject newMissile on line 81

    Line 82 (assuming Projectile_Missile is a valid type) try newMissile.GetComponent<Projectile_Missile>().myTarget = myTarget
     
  9. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    Projectile_Missile is the name of the script we are trying to get component from ... but sadly this does not work still getting an error in this line of code :( ... thanks though

    error CS1061: Type `UnityEngine.Object' does not contain a definition for `GetComponent' and no extension method

    I remember in the past doing first something like this when I wanted to get a component from a script on another object

    void Start ()
    {
    EnemyArray = Object.FindObjectsOfType (typeof (Projectile_Missile)) as projectile_Missile[];
    }

    but the thing is that I cant see anything like it in original js ... is it possible it is because js does not need to do that? I have no knowledge of js so find it hard to convert :(
     
    Last edited: Mar 18, 2015
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. var newMissile = Instantiate(...) as GameObject;
    3. newMissile.GetComponent<Projectile_Missile>().myTarget = myTarget;
    4.  
     
    Crayz likes this.
  11. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    55
    That's is working ... thank you so much ... great to learn something new

    I actually got it working this way by adding this to projectile script , but it was less efficient ;
    Code (CSharp):
    1. public Player player;
    2.  
    3.  
    4. public void Awake ()
    5.     {
    6.         GameObject playerObject = GameObject.FindWithTag ("Player") ;
    7.         if ( playerObject !=null)
    8.         {
    9.             player = playerObject.GetComponent<Player>();
    10.         }
    11.     }
    12.  
    13.  
    14. public void Update ()
    15.     {  
    16.  
    17.         myTarget = player.myTarget;
    18.                                  }
    19.