I’m teaching myself scripting through Unity, trying to focus on C# and understand how it differs with JS. As I work through the scripting tutorial (and beyond) that Unity provides (http://docwiki.unity3d.com/uploads/Main/Scripting%20Tutorial.pdf) I’ve been running into regular problems converting the provided JS code into C#.
As I’m a newb to both scripting and Unity, hopefully you folks can help me and any others running into the same issues in the future.
after figuring out the translations for the Move and Follow scripts from JS to C#, I’ve run into problems with the Switch script.
Here’s the tutorial language in JS:
var switchToTarget : Transform;
function Update () {
if (Input.GetButtonDown(“Jump”))
GetComponent(Follow).target = switchToTarget;
…And here’s my C# version:
void Start () {
}
public Transform switchToTarget;
void Update () {
if (Input.GetButtonDown(“Jump”))
{
GetComponent(Follow1).target = switchToTarget;
} }
I get an error with the GetComponent line-- my error message (“Expression denotes a type', where a
variable’, value' or
method group’ was expected”) tells me it has something to do with how I’m using target… but I’ve got no clue how to fix it. Thoughts?
Thanks,
dh
Please put your code in
tags.
[CODE]void Start () {
}
public Transform switchToTarget;
void Update () {
if (Input.GetButtonDown("Jump"))
{
GetComponent("Follow1").target = switchToTarget;
}
Does that work? Some of the type-related stuff is a little different between the two languages.
By the way, the Unity Scripting Reference is your friend. If you look here: Unity - Scripting API: GameObject.GetComponent you’ll see a couple examples, and you’ll see that GetComponent can take a string as a parameter.
Thanks for the tip re the
tags.
Unfortunately that didn't change things.
Before I added the quotes to [CODE]GetComponent("Follow1").target = switchToTarget;
, my error message was “Expression denotes a type', where a
variable’, value' or
method group’ was expected.”
With your changes my error message is now "Type UnityEngine.Component' does not contain a definition for
target’ and no extension method target' of type
UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)
Okay, that’s because you’re trying to access the target element of a component, and components don’t have that member. What are you trying to retrieve using GetComponent, is it a script?
If so, you’ll have to cast it to the appropriate type first. Example:
Follow1 follow1Object = (Follow1) GetComponent("Follow1");
Then you can access the target member using follow1Object.target.
Yes! That did it! Thanks!
Here’s the c# code for the Switch tutorial:
void Start () {
}
public Transform switchToTarget;
void Update () {
if (Input.GetButtonDown("Jump"))
{
Follow1 follow1Object = (Follow1) GetComponent("Follow1");
follow1Object.target = switchToTarget;
}
}
If the script is called Follow1.cs etc it is better to use the class name than a string.
GetComponent<Follow1>().target = switchToTarget;
personally I always do something more like robotfeelings, which lets you check if the component exists before you start accessing it.
Follow1 accessFollow1 = GetComponent<Follow1>();
if(accessFollow1) accessFollow1.target = switchToTarget;