there is no 'gameobject' attached to the 'other game object' but a script is trying to access it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
private CharacterController controller;
private Vector3 direction;
public float forwardSpeed;

private int desiredLane= 1;
public float laneDistance = 4;
void Start()
{
controller = GetComponent();
}

// Update is called once per frame
void Update()
{
direction.z = forwardSpeed;

if (Input.GetKeyDown(KeyCode.RightArrow));
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}

if (Input.GetKeyDown(KeyCode.LeftArrow));
{
desiredLane–;
if (desiredLane == -1)
desiredLane = 0;
}

Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;

if (desiredLane == 0)
{
targetPosition += Vector3.left * laneDistance;
}else if (desiredLane == 2)
{
targetPosition += Vector3.right * laneDistance;
}

transform.position = targetPosition;
}

private void FixedUpdate()
{
controller.Move(direction * Time.fixedDeltaTime);
}
}

please …

  • … stop making stupid polls
  • … use CODE tags when you post code
  • … describe your problem
  • … for errors: paste the exact error message including the number of the problematic line.
1 Like