See following
question #1
scripts of javascript:
function Update () {
GetComponent(Transform).Rotate(0,5,0);
}
Can run…
scripts of C#
using UnityEngine;
using System.Collections;
public class sy01_c_sharp : MonoBehaviour {
void Start () {
}
void Update () {
GetComponent(Transform).Rotate(0,5,0);
}
}
can’t run…
message : Expression denotes a’type’,where a ‘variable’,‘value’ or ‘method group’ was expected
---------------------------------
question #2
scripts of javascript:
function Start()
{
transform.Translate(0,1.5,0);
}
Can run…
scripts of C#
using UnityEngine;
using System.Collections;
public class sy01_c_sharp : MonoBehaviour {
void Start () {
transform.Translate(0 , 1.5 , 0);
}
void Update () {
}
}
can’t run…
message : The best overloaded method match for ‘UnityEngine.Transform.Translate(float,float,float)’ has some invalid arguments
Thanks a lot for any help…
1 case: GetComponent expects a type. Types are gathered through typeof()
2 case: 1.5 is no float, thats a double.
It should be 1.5F
o,thank you very much.
But again not run too in case #1:
Code is as follows
using UnityEngine;
using System.Collections;
public class sy01_c_sharp : MonoBehaviour {
void Start () {
}
void Update () {
GetComponent(typeof(Transform)).Rotate(0,5,0); //error
}
}
message:‘UnityEngine.Component’ does not contain a definition for ‘Rotate’.
well Getcomponent returns a Component, not a Transform.
so you would first need to cast it to Transform
but the solution for the standard components like Transform is simpler:
just use transform.Rotate(…)
on MonoBehaviour extended classes
that will do the same as retrieving the component and casting (its not stored in there as in a field), but it does so invisible behind the scene.
Thank you,I see…
((Transform)(GetComponent(typeof(Transform)))).Rotate(0,.5f,0);
Really is too cumbersome…
But I have no choice…
to thank again
just use transform.Rotate(…)
you do not need to use Getcomponent for the default components on game objects like the Transform