admin管理员组

文章数量:1390803

Need assistance with translating JS to C#.

Original script:

var target : GameObject;
var fadeDuration : float = 3.0;


function Update(){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

Translated C# script:

using UnityEngine;
using System.Collections;

public class FadeOutDeadBody : MonoBehaviour {
GameObject target;
float fadeDuration = 3.0f;
void Update (){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

}

I receive errors from Unity3d that say:

folder/FadeOutDeadBody.cs(9,29): error CS1525: Unexpected symbol <', expectingidentifier'

Any ideas how to fix this problem?

Need assistance with translating JS to C#.

Original script:

var target : GameObject;
var fadeDuration : float = 3.0;


function Update(){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

Translated C# script:

using UnityEngine;
using System.Collections;

public class FadeOutDeadBody : MonoBehaviour {
GameObject target;
float fadeDuration = 3.0f;
void Update (){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

}

I receive errors from Unity3d that say:

folder/FadeOutDeadBody.cs(9,29): error CS1525: Unexpected symbol <', expectingidentifier'

Any ideas how to fix this problem?

Share Improve this question asked Aug 17, 2016 at 20:50 papipapi 3899 silver badges26 bronze badges 3
  • 2 I'm voting to close this question as off-topic because StackOverflow isn't a coding service – Matías Fidemraizer Commented Aug 17, 2016 at 20:53
  • Disagree, it's a valid question with a demonstrated effort to solve before ing here for help. – saarrrr Commented Aug 17, 2016 at 20:56
  • Then it's a quality issue or he should open a new question, not off-topic. – saarrrr Commented Aug 17, 2016 at 21:02
Add a ment  | 

5 Answers 5

Reset to default 6

Just delete your dot in GetComponent.<Renderer> => GetComponent<Renderer>

Everyone left this out so I decided to post.

You cannot modify the alpha of a color directly. You have to create a new color, modify the alpha, then assign it back to your Material color. Also remove the dot too. This is what it should look like:

public class FadeOutDeadBody : MonoBehaviour
{
    public GameObject target;
    float fadeDuration = 3.0f;

    Renderer renderer;

    void Start()
    {
        renderer = target.GetComponent<Renderer>();
    }

    void Update()
    {

        if (renderer.material.color.a > 0)
        {
            Color color = renderer.material.color;
            color.a -= Time.deltaTime / fadeDuration;
            renderer.material.color = color;
        }
    }
}

GetComponent.<Renderer>() -> GetComponent<Renderer>() You have extra '.' in there that is throwing the piler off.

Edit to respond to your ment on romain-aga's answer

You need to expand it to look something like this (C# works a bit differently than JS so you can't be as flexible)

Renderer r = target.GetComponent<Renderer>();
Color c = r.material.color;
c.a -= Time.deltaTime/fadeDuration;
r.material.color = c;

The r, g, b, and a properties of a color in C# are read-only, so you have to modify the entire color value.

Note: I don't have Unity on the puter I'm at right now, so this is untested. We'll see how well my memory is working today.

Instead of this

target.GetComponent.<Renderer>()

you probably need this

target.GetComponentInParent<Renderer>()

or this

target.GetComponent(typeof(Renderer))

Just be careful when "translating" code from one language to another, as conventions are language specific. Looking at the unity docs, the proper way to call GetComponent is as a generic, so you can fix this particular issue by simply removing the period in your code calling GetComponent.

In general, you will probably have more problems like this due to C# being very different from JavaScript, so just be aware of this.

Reference: GetComponent documentation

本文标签: javascriptTranslating js to CStack Overflow