how to convert to C#

var instantGrenade : Rigidbody = Instantiate(grenade, spawnPos.posiotion, spawnPos.rotation);
Rigidbody instantGrenade = Instantiate(grenade, spawnPos.position, spawnPos.rotation) as Rigidbody;

That is IF ‘grenade’ is a Rigidbody going in.

1 Like
function ThrowGrenade (power : float){
    if(throwingGrenade || grenadeCount <= 0)
    return;
  
    throwingGrenade  = true;
    weaponAnim.GetComponent.<Animation>()[grenadeThrow].speed = weaponAnim.GetComponent.<Animation>()[grenadeThrow].clip.length/0.4;
    weaponAnim.GetComponent.<Animation>().Play(grenadeThrow);
    yield WaitForSeconds (0.2);
    grenadeRenderer.enabled = false;
    var instantGrenade : Rigidbody = Instantiate(grenade, spawnPos.position, spawnPos.rotation);
   
    var fwd : Vector3 = spawnPos.forward;
    instantGrenade.AddForce(fwd * power * throwForce);
    //Physics.IgnoreCollision(instantGrenade.collider, transform.root.collider);
    //Physics.IgnoreCollision(instantGrenade.collider, gameObject.FindWithTag("Player").transform.root.collider);
    grenadeCount--;
   
    yield WaitForSeconds(1.0);
    grenadeRenderer.enabled = true;
    throwingGrenade = false;
    DrawWeapon();   
}

sorry this is the whole function

@TRowe007

Is this a sort of post where you actually are asking people to translate your UnityScript to C#?

I bet it would be easier to learn differences between UnityScript and C# than ask for favors… and also learn basics of C#… It doesn’t take too long actually. I’m slow learner and I managed to do that…

If you actually took a look at @lordofduct 's answer, you’d notice only major differences are related to variables and types (in scope that you are trying to tranlate basic scripts from language to language).

In Unity script you have to use:
var - variableName - semicolon - type
var instantGrenade : Rigidbody;

In c# it’s nearly the same but in different order:
type - variabableName
Rigidbody instantGrenade;

and in addition to this, you can/have to define access modifiers (public, private, protected…):
public Rigidbody instantGrenade;

… and for methods you don’t need the keyword function, but you need the return type:

US: function ThrowGrenade (power : float){}

C#: void ThrowGrenade (float power) {}

But it’s better check these differences from Unity learning material, but these will get you quite far already IMO. I just wonder have you actually checked Unity manual or training material?

In manual and tutorials, you can compare / swap between languages - also, there are like 113 tutorials there on scripting…

i gave you the answer to this function yesterday. did you not see it?

here it is again

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadePickup : MonoBehaviour
{
    //ANIMATIONS
    public GameObject weaponAnim;
    public string idleAnim = "Idle";
    public string DrawAnimation = "Draw";
    public string grenadeThrow = "GrenadeThrow";
    public string grenadePull = "GrenadePull";
    public AudioClip soundDraw;
    //GRENADE
    public int grenadeCount = 3;
    public float throwForce = 15.0f;
    public float powerIncreaseSpeed = 80.0f;
    private float powerBarFullWidth = 100.0f;
    private float thePower = 0.0f;
    private bool increasing = false;
    private bool throwingGrenade = false;
    public Rigidbody grenade;
    public Transform spawnPos;
    public Renderer grenadeRenderer;
    //CROSSHAIR
    public Texture2D crosshair;
    //GUI
    public GUISkin mySkin;
    public bool selected = false;
    void Start()
    {
        weaponAnim.GetComponent<Animation>()[grenadePull].wrapMode = WrapMode.ClampForever;
    }
    void Update()
    {
        if (selected)
        {
            if (grenadeCount > 0 && !throwingGrenade)
            {
                if (Input.GetButtonDown("Fire"))
                {
                    if (thePower < 5)
                    {
                        weaponAnim.GetComponent<Animation>().Stop();
                        weaponAnim.GetComponent<Animation>()[grenadePull].speed = 1.0f;
                        weaponAnim.GetComponent<Animation>().CrossFade(grenadePull);
                        increasing = true;
                    }
                }
                if (Input.GetButtonUp("Fire"))
                {
                    increasing = false;
                    if (thePower > powerBarFullWidth / 2)
                    {
                        ThrowGrenade(thePower); //error, this does not exsist
                    }
                    else
                        weaponAnim.GetComponent<Animation>()[grenadePull].speed = -1.0f;
                    weaponAnim.GetComponent<Animation>().CrossFade(grenadePull);
                }
            }
            if (thePower < 0)
            {
                thePower = 0;
            }
            if (increasing)
            {
                thePower += Time.deltaTime * powerIncreaseSpeed;
                thePower = Mathf.Clamp(thePower, 0, powerBarFullWidth);
            }
            else
            {
                if (thePower > 0)
                    thePower -= Time.deltaTime * powerIncreaseSpeed * 2;
            }
        }
    }
    void OnGUI()
    {
        GUI.skin = mySkin;
        var style1 = mySkin.customStyles[0];
        GUI.Label(new Rect(Screen.width - 200, Screen.height - 35, 200, 80), "Grenades : ");
        GUI.Label(new Rect(Screen.width - 110, Screen.height - 35, 200, 80), "" + grenadeCount, style1);
        GUI.Label(new Rect(Screen.width - 200, Screen.height - 65, 200, 80), "Throwing Power : ");
        GUI.Label(new Rect(Screen.width - 70, Screen.height - 65, 200, 80), "" + thePower.ToString("F0") + "%", style1);
        if (crosshair != null)
        {
            var w = crosshair.width / 2;
            var h = crosshair.height / 2;
            Rect position = new Rect((Screen.width - w)/2,(Screen.height - h )/2, w, h); // <<<<< Fixed
            GUI.DrawTexture(position, crosshair);
        }
    }
IEnumerator ThrowGrenade (float power) // In Jave power works fine but in C# it doesn't seem to want to.<<<<<
{
    if(throwingGrenade || grenadeCount <= 0)
    yield break;
    throwingGrenade = true;
    weaponAnim.GetComponent<Animation>()[grenadeThrow].speed = weaponAnim.GetComponent<Animation>()[grenadeThrow].clip.length / 0.4f;
    weaponAnim.GetComponent<Animation>().Play(grenadeThrow);
        yield return new WaitForSeconds(0.2f);
        grenadeRenderer.enabled = false;
        Rigidbody instantGrenade = (Rigidbody) Instantiate(grenade, spawnPos.position, spawnPos.rotation);
        Vector3 fwd = spawnPos.forward;
    instantGrenade.AddForce(fwd* power * throwForce);
    //Physics.IgnoreCollision(instantGrenade.collider, transform.root.collider);
    //Physics.IgnoreCollision(instantGrenade.collider, gameObject.FindWithTag("Player").transform.root.collider);
    grenadeCount--;
    yield return new WaitForSeconds(1.0f);
        grenadeRenderer.enabled = true;
    throwingGrenade = false;
    DrawWeapon();
}
IEnumerator DrawWeapon()
{
    thePower = 0;
    grenadeRenderer.enabled = true;
    increasing = false;
    bool draw = true;
    if (grenadeCount > 0)
    {
        GetComponent<AudioSource>().clip = soundDraw;
        GetComponent<AudioSource>().Play();
        weaponAnim.GetComponent<Animation>()[DrawAnimation].speed = weaponAnim.GetComponent<Animation>()[DrawAnimation].clip.length / 0.9f;
        weaponAnim.GetComponent<Animation>().Play(DrawAnimation);
        yield return new WaitForSeconds(0.6f);
    }
    selected = true;
}
void Deselect()
{
    selected = false;
}
}
1 Like

oh maybe my bad ok thank you

let me know if you need any more help

1 Like