1

I'm a new Unity user, and I'm just trying some script in the Unity 2D game example. Now I would like to make a portal to the game, and I have found a script for it on the internet, but it has been written in UnityScript, but my game is in C#, so I would like to write it to C#. I have a problem with the getComponent method as I get error if I use it as it is in JS. I would like to ask you that what should I write instead the GetComponent, or how should I write it.

Here's the JS script:

var target : GameObject;
var adjust : float;
var jump   : boolean;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

function OnTriggerEnter(other : Collider) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent(Teleport).jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

function OnTriggerExit(other : Collider) {
    if (other.tag == "Player") {
        jump = false;
    }
}
}

I get these errors, on the next code:

    public float adjust;
    public object target;
    public bool jump;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter(Collider other) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent(Teleport).jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

void OnTriggerExit(Collider other) {
    if (other.tag == "Player") {
        jump = false;
    }
}
}

Type object' does not contain a definition forGetComponent' and no extension method GetComponent' of typeobject' could be found (are you missing a using directive or an assembly reference?)

Expression denotes a type', where avariable', value' ormethod group' was expected

If you can help me, maybe with the working C# script of this code, then it would be great.

EDIT:

I have tried what you said so my Teleport script looks like this now:

public class Teleport : MonoBehaviour {
public float adjust;
public GameObject target;
public bool jump;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter(Collider other) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent<Teleport>.jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

void OnTriggerExit(Collider other) {
    if (other.tag == "Player") {
        jump = false;
    }
}

}

if I use the new Vector2 then it gives me 5 errors:

- Expression denotes a `method group', where a `variable', `value' or `type' was expected
- Type `UnityEngine.GameObject' does not contain a definition for `position' and no      extension method `position' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
- Type `UnityEngine.GameObject' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
-  The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments
- Argument `#1' cannot convert `object' expression to type `float'

Without the new operator I got two errors:

- Expression denotes a `method group', where a `variable', `value' or `type' was expected
- Expression denotes a `type', where a `variable', `value' or `method group' was expected

EDIT2: I have noticed that for some reason I don't see the target, adjust variables in the inspector at the Teleport script component.

EDIT3: Now I can run it without errors (I have made small mistakes), but it isn't work for me. If I go to the "portal" with the character then nothing happens. What can be wrong? I have added the other "portal" in the inspector to the script.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Zwiebel
  • 1,605
  • 4
  • 21
  • 37
  • Try getting logs inside the trigger to check whether you r getting inside that or not. – Nick Feb 24 '14 at 04:37

1 Answers1

5

Use this.

public GameObject target;

target.GetComponent<Teleport>().jump = true;

Refer this.

Also correct this with

other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);
Community
  • 1
  • 1
Nick
  • 1,035
  • 1
  • 11
  • 18
  • 1
    Also I think target type needs to be changed to GameObject from object, because later on in the code target is trying to invoke GetComponent method which is not available for object. Am I right? – martynaspikunas Feb 22 '14 at 11:51