Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Type name instead of instance reference

Discussion in 'Scripting' started by Crazybrownie204, Oct 27, 2021.

  1. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    This error keeps coming up over and over, no matter what I try, and I haven't been able to wrap my head around the problem. I am trying to change a Transform from another GameObject to a Vector3, but nothing is working. Please help me! The actual error says:
    Assets\Scripts\PlayerMovement.cs(36,41): error CS0176: Member 'Vector3.forward' cannot be accessed with an instance reference; qualify it with a type name instead
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Show us your code (use code tags READ THIS).
    My blind guess is that you're trying to overwrite the
    transform
    with a
    Vector3.forward
    . You probably want to overwrite the
    transform.position
    , but we only can be sure if you show us your code.
     
    Bunny83 likes this.
  3. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private Vector3 PlayerCameraRotation;
    8.     [SerializeField] private Transform PlayerCamera;
    9.     void Update(){
    10.         if(Input.GetMouseButton(0)){
    11.             PlayerCameraRotation = new Vector3(PlayerGroundCheck.transform.position.x, PlayerGroundCheck.transform.position.y, PlayerGroundCheck.transform.position.z);
    12.             PlayerBody.AddRelativeForce(PlayerCameraRotation.forward * PlayerThrust);
    13.         }
    14.     }
    15. )
    And by the way yes, I have correctly connected the PlayerCamera Transform
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    This should be
    Vector3.forward
    because it is simply a static member returning a
    new Vector3(0, 0, 1f);
     
    Vryken likes this.
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    This is also redundant:
    Code (CSharp):
    1. PlayerCameraRotation = new Vector3(PlayerGroundCheck.transform.position.x, PlayerGroundCheck.transform.position.y, PlayerGroundCheck.transform.position.z);
    It can be simplified to:
    Code (CSharp):
    1. PlayerCameraRotation = PlayerGroundCheck.transform.position;
     
  6. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    My problem is that I want the current object to have added force going in the forward direction of PlayerCamera. Is there any way I can do that? It would save a lot of headaches. Although thank you for your replies.
     
  7. herrmutig

    herrmutig

    Joined:
    Nov 30, 2020
    Posts:
    22
    Firstly. You get that error because PlayerCameraRotation is a Vector3 and not a Transform. A Vector3 is just a point in space or a direction. It does not have a forward attribute when you use it as an instance of an object. If you use it as a static class member, than you have access to Vector3.forward which is the same as new Vector3(0,01)


    For your Problem:
    You need the transform of the PlayerCamera which you already have in your code at line 8. So replace the followin

    1. PlayerBody.AddRelativeForce(PlayerCameraRotation.forward * PlayerThrust);

    with this:
    PlayerBody.AddRelativeForce(PlayerCamera.forward * PlayerThrust);
     
  8. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    Thank you, that is really helpful! It is working now.