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
<', expecting
identifier'
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
<', expecting
identifier'
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
5 Answers
Reset to default 6Just 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
版权声明:本文标题:javascript - Translating js to C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744720536a2621666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论