1

Let me start by saying that I am a complete beginner, who only got this far by piecing together parts from tutorials and sites like this.

All I am trying to do is make a platform that moves only when stood on.

using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour
{

public bool isPlayerOn;


[SerializeField]
Transform platform;

[SerializeField]
Transform startTransform;

[SerializeField]
Transform endTransform;

[SerializeField]
float platformSpeed;

Vector3 direction;
Transform destination;


void OnCollisionEnter (Collision other) {

    if (Collision.gameObject.tag == "Player") {
        isPlayerOn = true;
    }

}


void OnCollisionExit (Collision other) {

    if (Collision.gameObject.tag == "Player") {
        isPlayerOn = false;
    }

}

void Start (){
    SetDestination (startTransform);
}



void FixedUpdate(){
    platform.rigidbody.MovePosition (platform.position + direction * platformSpeed * Time.fixedDeltaTime);

    if (Vector3.Distance (platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime) {
        SetDestination (destination == startTransform ? endTransform : startTransform);
    }
}


void SetDestination(Transform dest){
    destination = dest;
    direction = (destination.position -platform.position).normalized;
}

}

Now I have no idea if this will even work, I managed to get rid of about 9 other errors but not this one.

Assets/MovingPlatform.cs(31,31): error CS0120: An object reference is required to access non-static member `UnityEngine.Collision.gameObject'

Steven
  • 166,672
  • 24
  • 332
  • 435

1 Answers1

1

Trying changing the code to:

void OnCollisionEnter (Collision other) {

    if (other.gameObject.tag == "Player") {
        isPlayerOn = true;
    }

}

instead of:

if (Collision.gameObject.tag == "Player") {

The error is telling you that there is no static member called gameObject on the Collision class. To call non-static members, you need an object reference. In this case, it looks like the object reference should be the other instance passed in to this method.

You may also want to do a few searches on static vs. non-static. There are some good answers here: Static vs non-static class members

Community
  • 1
  • 1
rsbarro
  • 27,021
  • 9
  • 71
  • 75