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

Instance reference problem

Discussion in 'Scripting' started by SirVoomy, Mar 1, 2021.

  1. SirVoomy

    SirVoomy

    Joined:
    Mar 1, 2021
    Posts:
    3
    SOLVED.
    Thank you PraetorBlue for helping me out!


    I'm trying to make teleporters and I have the base code mostly set up but I'm running into some error issues. Some way to fix the error or make my code better would be very appreciated.
    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class teleporters : MonoBehaviour
    {
    public Vector3 teleporterExit;


    // Start is called before the first frame update
    void Start()
    {
    teleporterExit = gameObject.Find("teleportExit").GetComponent.transform();
    }

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

    }

    public void OnTriggerEnter(Collider other)
    {
    if(other.gameObject.tag == "TeleporterEntrance")
    {
    transform.position = teleporterExit;
    }
    }
    }

    Error message:
    Assets\teleporters.cs(13,26): error CS0176: Member 'GameObject.Find(string)' cannot be accessed with an instance reference; qualify it with a type name instead
     
    Last edited: Mar 1, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    gameObject
    is a property of your MonoBehaviour which refers to the GameObject your MonoBehaviour is attached to.

    GameObject
    is the name of the GameObject class.

    In order to call a static method such as GameObject.Find(), you must use the class name to call the method. That's because static methods are not related to a specific instance of a class, they are part of the class itself.

    You can tell Find is a static method because it is marked as static in the documentation page: https://docs.unity3d.com/ScriptReference/GameObject.Find.html

    To fix your issue you need to just change
    gameObject.Find("teleportExit")
    to
    GameObject.Find("teleportExit")
    with a capital G.
     
    mopthrow likes this.
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Please use code tags, it's easier to read and we haven't got to count the lines to find out the line number :)

    Probably typo on 13, pretty sure it's GameObject.Find() not gameObject.Find().
     
  4. SirVoomy

    SirVoomy

    Joined:
    Mar 1, 2021
    Posts:
    3
    See what happens with that is I have to do <Transform> instead of .transform and then it gives me this error:
    ArgumentException: GetComponent requires that the requested component 'Vector3' derives from MonoBehaviour or Component or is an interface.
    UnityEngine.GameObject.GetComponent[T] () (at <31d5d65b32ec483292e13e8ae4100b93>:0)
    teleporters.Start () (at Assets/teleporters.cs:13)

    and this error:
    Assets\teleporters.cs(13,26): error CS0029: Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.Vector3'
     
  5. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    A Transform component itself is not of type Vector3. It's of type Transform. You probably want GetComponent<Transform>.position.