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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems with get component

Discussion in 'Scripting' started by RobotReebot, Jun 10, 2015.

  1. RobotReebot

    RobotReebot

    Joined:
    Jul 31, 2014
    Posts:
    13
    Hi,
    I have a Main Camera with a Camera 2D Follow Component. I want to change the target of this script by script, using get component, but Get component don't find "Camera2DFollow" As a component of the main camera.

    How i can solve this?

    Code (CSharp):
    1. //this script is attached to Main Camera//
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class CreaNavicellaRandom : MonoBehaviour {
    7.     int rnd;
    8.     public GameObject Arancione;
    9.     public GameObject Azzurro;
    10.     public GameObject Bianco;
    11.     public GameObject Blu;
    12.     public GameObject Giallo;
    13.     public GameObject Nero;
    14.     public GameObject Rosso;
    15.     public GameObject Rosa;
    16.     public GameObject Verde;
    17.     public GameObject Viola;
    18.     // Use this for initialization
    19.     // Da aggiunger la posizione Randomica una volta detereminato lo spazio in cui possono muoversi le cellule
    20.     void Start () {
    21.         rnd=Random.Range(1,11);
    22.         switch (rnd) {
    23.         case 1:
    24.             Instantiate(Arancione);
    25.             break;
    26.         case 2:
    27.             Instantiate(Azzurro);
    28.             break;
    29.         case 3:
    30.             Instantiate(Bianco);
    31.             break;
    32.         case 4:
    33.             Instantiate(Blu);
    34.             break;
    35.         case 5:
    36.             Instantiate(Giallo);
    37.             break;
    38.         case 6:
    39.             Instantiate(Nero);
    40.             break;
    41.         case 7:
    42.             Instantiate(Rosso);
    43.             break;
    44.         case 8:
    45.             Instantiate(Rosa);
    46.             break;
    47.         case 9:
    48.             Instantiate(Verde);
    49.             break;
    50.         case 10:
    51.             Instantiate(Viola);
    52.             break;
    53.  
    54.         }
    55.         GetComponent<Camera2DFollow> ().target = GameObject.FindGameObjectWithTag ("Player").transform;
    56.     }
    57.    
    58.     // Update is called once per frame
    59.     void Update () {
    60.    
    61.     }
    62. }
    63.  
     
  2. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    this will work only if that instance of the class CreaNavicellaRandom is a component of the camera.

    If CreaNavicellaRandom is not a component of the camera and your camera is tagged as main camera you could use Camera.main.transform.GetComponent<Camera2DFollow>().target

    (edit) if both scripts are components of the main camera it should work

    (second edit) unless you're instantiating your player object on start and script execution order is not on your side... you should make sure that the player object already exists when the start method gets called.
     
    Last edited: Jun 10, 2015
  3. RobotReebot

    RobotReebot

    Joined:
    Jul 31, 2014
    Posts:
    13
    I tried to put that function in awake beacouse camera start on void start, but still don't work
     
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Just to be sure: "Get component don't find 'Camera2DFollow'" means there's a runtime error like NullReferenceException: Object reference not set to an instance of an object or something?

    Or are we talking about error CS0246: The type or namespace name `Camera2DFollow' could not be found. Are you missing a using directive or an assembly reference?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Out of sheer curiosity.... did you type those errors out from memory? lol
     
  6. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Of course not. :)