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

Question How to get value of component from Main Camera?

Discussion in 'Scripting' started by lw_wh, Jun 12, 2023.

  1. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    Hello,

    I'm trying to get a value of a component of the Main Camera. The Main Camera has a MainCamera tag.
    But i can't reach the value of the longitude.
    Code (CSharp):
    1.     public GameObject test;
    2.     void Update()
    3.     {
    4.     test = gameObject.FindWithTag("MainCamera").GetComponent("ArcGIS Location").longitude;
    5.     Debug.Log(test);
    6. }
    upload_2023-6-12_12-51-35.png

    I use (" ") because of the spaces in the component name, but i don't know for sure if thats the correct way.

    Hope someone can give me a tip.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Don't get it by name, get it by type. Use "GetComponent<ComponentType>()" as per the docs.
     
    angrypenguin and mopthrow like this.
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Sure, there's a bit of a shorthand to get the main camera.
     Camera.main
    will get the camera with the Main Camera tag.

    For the GetComponent you're close, but you want
    GetComponent<ArcGISLocation>();


    Something like this:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Testing : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         Camera mainCamera = Camera.main;
    8.         ArcGISLocation gisLocation = mainCamera.GetComponent<ArcGISLocation>();
    9.         float longitude = gisLocation.longitude;
    10.         Debug.Log(longitude);
    11.     }
    12. }
    Also, generally you'd cache GetComponent calls and any Find stuff in Start or Awake as Update will do that every frame. It can get costly if you have a lot of these scripts in your scene, so do it once and use as often as you want. Something like:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Testing : MonoBehaviour
    4. {
    5.     private ArcGISLocation gisLocation;
    6.  
    7.     private void Start()
    8.     {
    9.         gisLocation = Camera.main.GetComponent<ArcGISLocation>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.F))
    15.         {
    16.             Debug.Log(gisLocation.longitude);
    17.         }
    18.     }
    19. }
    Make sure your ArcGISLocation doesn't have spaces in either the file name or the class. It's fine if it shows spaces in the name of the script in the inspector. It's just Unity trying to format it nicely so it's more readable.

    Make sure longitude is public or has a public getter.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Really, just don't write code like this. It's so unnecessary most of the time.

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.


    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();
     
  5. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    Hello, i think this is a good way, but i get the error message that the ArcGISLocation is missing. I think of the space in the component name.


    Also, generally you'd cache GetComponent calls and any Find stuff in Start or Awake as Update will do that every frame. It can get costly if you have a lot of these scripts in your scene, so do it once and use as often as you want. Something like:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Testing : MonoBehaviour
    4. {
    5.     private ArcGISLocation gisLocation;
    6.  
    7.     private void Start()
    8.     {
    9.         gisLocation = Camera.main.GetComponent<ArcGISLocation>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.F))
    15.         {
    16.             Debug.Log(gisLocation.longitude);
    17.         }
    18.     }
    19. }
     
  6. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
     
    Last edited: Jun 13, 2023
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,882
    What do you mean missing? You mean a Null Reference exception?

    That would imply the component is not on the camera, or you're trying to get the component from the wrong game object.
     
    mopthrow likes this.
  8. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    error CS0246: The type or namespace name 'ArcGISLocation' could not be found (are you missing a using directive or an assembly reference?) but the ArcGIS Location component is in the
    Main Camera.

    When i use Camera.main.GetComponent<Transform>(); And then Debug.Log(gisLocation.position.y) i get a normal reaction. And Transform is also under the Main Camera.
     
    Last edited: Jun 13, 2023
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,882
    Does your IDE not help auto complete the right component name?

    Or I would look at the documentation of whatever tool you're using to see what the actual component names are.
     
  10. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    Thanks. I opened the component script and found there the public class name: ArcGISLocationComponent
    I tried that name with Debug.Log(gisLocation.Position.Y); And YES i get upload_2023-6-13_9-28-6.png

    Now i can call the position of the player. Because i think the position changed when the player/camera moves.
     
  11. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    In the script you quote in post #5, change all your ArcGISLocation to ArcGISLocationComponent to match the class name.

    Not sure why the inspector is saying one thing and the class name is something else. Kinda strange.
     
  12. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    Yeah strange. Maybe that's a setting in the ArcGIS SDK plugin i use. I also thought the component name must always be the same as the class name. Looking back it was a easy fix:)
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,128
    You're missing the necessary using statements in that example. Your IDE should have suggested them to you.
     
  14. lw_wh

    lw_wh

    Joined:
    Nov 1, 2021
    Posts:
    21
    I have another little question. With this code i can access x and y, but not Spatial Ref WKID
    Code (CSharp):
    1. var GisLocation = CoorXY.GetComponent<ArcGISLocationComponent>();
    2.         var GisX = GisLocation.Position.X.ToString();

    upload_2023-6-14_11-52-3.png

    I tested with:
    Code (CSharp):
    1. var GisLocation = CoorXY.GetComponent<ArcGISLocationComponent>();
    2.         var GisX = GisLocation.Position.SpatialRefWKID.ToString();
    cant found it
     
    Last edited: Jun 14, 2023