Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 'Vector3' does not contain a definition for 'transform'

Discussion in 'Getting Started' started by MXTRS, Nov 20, 2023.

  1. MXTRS

    MXTRS

    Joined:
    Aug 7, 2023
    Posts:
    2
    Hey guys! I'm having some problems with my code, I review it several times but couldn't find where it's wrong. This code is for a player movement from an asset.


    FirstPersonMovement.cs(28,44): error CS1061: 'Vector3' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'Vector3' could be found (are you missing a using directive or an assembly reference?)



    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5.  
    6. public class FirstPersonMovement : MonoBehaviour
    7. {
    8.     public float speed = 5;
    9.  
    10.     [Header("Running")]
    11.     public bool canRun = true;
    12.     public bool IsRunning { get; private set; }
    13.     public float runSpeed = 9;
    14.     public KeyCode runningKey = KeyCode.LeftShift;
    15.     static public bool dialogue = false;
    16.     public Respawnpos respawnpos; //vector3 reference from another script
    17.     public Vector3 Spawn;
    18.     Rigidbody rigidbody;
    19.     /// <summary> Functions to override movement speed. Will use the last added override. </summary>
    20.     public List<System.Func<float>> speedOverrides = new List<System.Func<float>>();
    21.  
    22.  
    23.  
    24.     public void Start()
    25.            {
    26.         // Get the rigidbody on this.
    27.         rigidbody = GetComponent<Rigidbody>();
    28.         Spawn = respawnpos.respawnPosition.transform.position;
    29.         //respawnPosition = gameManager.spawnpoint.transform.position;
    30.            }
    31.  
    32.  
    33.  
    34.     void FixedUpdate()
    35.     {
    36.         if (!dialogue)
    37.         {
    38.             IsRunning = canRun && Input.GetKey(runningKey);
    39.  
    40.             // Get targetMovingSpeed.
    41.             float targetMovingSpeed = IsRunning ? runSpeed : speed;
    42.             if (speedOverrides.Count > 0)
    43.             {
    44.                 targetMovingSpeed = speedOverrides[speedOverrides.Count - 1]();
    45.             }
    46.  
    47.             // Get targetVelocity from input.
    48.             Vector2 targetVelocity = new Vector2(Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);
    49.  
    50.             // Apply movement.
    51.             rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
    52.         }
    53.  
    54.     }
    55. }



    There's another script that is referenced in the first one


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Respawnpos : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public GameObject spawn;
    9.     public Vector3 respawnPosition;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     public void Start()
    14.     {
    15.         respawnPosition = player.transform.localPosition;
    16.     }
    17.  
    18.     public void OnTriggerEnter(Collider collider)
    19.     {
    20.         if (collider.gameObject.CompareTag("Player"))
    21.         {
    22.             respawnPosition = this.transform.localPosition;
    23.         }
    24.     }
    25.  
    26. }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,288
    FirstPersonMovement.cs(28,44):
    means line 28 around column 44.

    28.  Spawn = respawnpos.respawnPosition.transform.position;


    'Vector3' does not contain a definition for 'transform'


    Your statement is using .transform but the thing to the left (respawnpos.respawnPosition) is a Vector3. There is no transform unless you're talking about a component or gameobject. Since you ultimately are trying to figure out a Vector3 for Spawn, I think you just want this.

    28.  Spawn = respawnpos.respawnPosition;


    It would be good to go through a basic C# course to get more comfortable with understanding variable types.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,587
    The error says exactly what the problem is- there's no such thing as Vector3.transform.

    Here is the API for transform:
    https://docs.unity3d.com/ScriptReference/Vector3.html

    If respawnpos.respawnPosition is a position then just use that. respawnpos.respawnPosition.transform.position. doesn't make any sense.
     
  4. MXTRS

    MXTRS

    Joined:
    Aug 7, 2023
    Posts:
    2
    Thanks for the replies guys! I'm still new to coding and taking some classes, I was wondering why it wasn't working since in class we did a similar code, I need study more lol. Anyway, thanks for helping me out!