Unity can't find a gameobject and shows an error instead?

Hello! I’m making a car game. Right now I’m trying to add a car selection system. So far I have 3 scripts. CarSelect.cs, DataHandler.cs and DataImplementer.js. CarSelect and DataHandler are used as one car selector in the menu/garage, for example - I have 3 cars so far. I put all car prefabs in one place, so when I click to select car A, car B and C disappear, and when I select car B, car A and C disappear. And then DataImplementer does that in the actual “gameplay” scene. So if I have car B selected in the menu scene, it will appear in the gameplay scene instead of car B and C. By the way, all 3 cars are placed just like in menu scene, in the same position.

So the problem is, DataImplementer finds the GameObject “DataHandler” which contains the “DataHandler” script (they are named the same), and so car A has ID of 1, car B - 2 and car C - 3. So after finding it must find the selected car variable int (1,2 or 3). So if its car A, the variable is set to 1, so it will affect the gameplay scene. Everything is simple in the scripts and it should work with no problems, but unity won’t find the GameObject called “DataHandler” for some really weird reason. Each script has its own gameobject in the same menu scene, and car variables and buttons are already set.

Here is the DataImplementer.js script:

#pragma strict

var carSelected : int;

var Kapri : GameObject;
var BMM : GameObject;
var BMMD : GameObject;


function Start () {

    carSelected = GameObject.Find("DataHandler").GetComponent(DataHandler).carSel;
   
    if (carSelected == 1) {
        Kapri.SetActive (true);
        BMM.SetActive (false);
        BMMD.SetActive (false);
    }
  
    else if (carSelected == 2) {
  
        Kapri.SetActive (false);
        BMM.SetActive (true);
        BMMD.SetActive (false);
    }
  
    else if (carSelected == 3) {
  
        Kapri.SetActive (false);
        BMM.SetActive (false);
        BMMD.SetActive (true);
    }
}

function Update () {

}

Error at line GameObject.Find("DataHandler").GetComponent(DataHandler).carSel;

Unity error says: Assets/Standard Assets/DataImplementer.js(12,63): BCE0005: Unknown identifier: 'DataHandler'.

I don’t think it’s needed to post other scripts since they function like they should and cause no errors.

So people, please tell me whats wrong here? I have no idea at all. And I’m not a pro coder, just a beginner (noob). If more info is required then please tell me which exactly (scripts,screenshots etc). Thanks!
(Kapri, BMM, BMMD - are the 3 cars I was talking about)

Looking at the Javascript version of getcomponent and you will probably need to cast it before you can get the value out of it.

I’d venture a guess and say that it’s because you’re mixing languages. UnityScript and C# are compiled at different times into different DLLs. Best bet is to be consistent on your language within a project; otherwise you’ll have to monkey with file locations in an attempt to get a (brittle) compile order that works.

1 Like

Alright I rewrote the DataImplementer script in CS, and everything is fine except for this line

    public int carSelected = GameObject.Find ("DataHandler").GetComponent(DataHandler).carSel;

Assets/Standard Assets/DataImplementer.cs(10,72): error CS0118: DataHandler' is a type’ but a `variable’ was expected

Assets/Standard Assets/DataImplementer.cs(10,72): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Can you please help me rewrite this line in CS? As I said Im a noob in scripting.

Use the generic overload of GetComponent

GetComponent<DataHandler>().carSel;

Oh, thank you so much man! Gonna test it in a minute.

Bad results… variable carSelected does not change with carSel. What’s wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataImplementer : MonoBehaviour {

    public GameObject Kapri;
    public GameObject BMM;
    public GameObject BMMD;
    public int carSelected = GameObject.Find("DataHandler").GetComponent<DataHandler>().carSel; //THX KelsoMRK!!!
    // Use this for initialization
    void Start () {
        if (carSelected == 1) {
            Kapri.SetActive (true);
            BMM.SetActive (false);
            BMMD.SetActive (false);
        }

        else if (carSelected == 2) {
            Kapri.SetActive (false);
            BMM.SetActive (true);
            BMMD.SetActive (false);
        }
        else if (carSelected == 3) {
            Kapri.SetActive (false);
            BMM.SetActive (false);
            BMMD.SetActive (true);
        }

    }

    void Update () {

    }

    void Awake () {

        GameObject.DontDestroyOnLoad (this.gameObject);
    }
}

Put the call to Find/GetComponent in Start

1 Like

Hello KelsoMRK, sorry for not being here for a while. So, as you said, I put GameObject Find string to the void Start, and the script seems to be fine now. But the same issue appears, when I select different cars with buttons, carSel from DataHandler and carSelected from CarSelect both change, but not carSelected from DataImplementer. Any idea why this happens?
PS I really feel guilty for wasting your time though, but thanks for the help so far!

First question would be why you’re holding the value of the selected car in three different places to begin with. :slight_smile:

because I’m a noob in coding. You know, actually I think of using just one script and one scene for garage and free roam (forgot to tell that I have it) like pretty much all NFS games for example do. That would be much easier to write and use. I’m gonna try to make the all-in-one script. I’ll post the results after I’m done.

That’s probably not much better. Generally speaking, a class (script) should be responsible for doing one thing. If you have a class that does player movement and data loading and car selection then that class is trying to do too many things.

Alright. So, I basically remade the garage scene in the freeroam scene with all buttons and their functions. Also, last time I forgot to add the DataHandler function thing to the buttons, but that doesn’t matter. What matters now is that when I select the cars, DataHandler and CarSelector change, but not DataImplementer. It stays at 1, so the Kapri car activates. What’s wrong? I even tried to put GameObject.Find string to all "If"s and "else if"s in DataImplementer instead of “if carSelected = 1” etc. Still doesn’t work… :frowning: Thanks for the help so far though!
PS DataHandler got rid of the Dont destroy on load function, just in case…

Wait! I tried to make All In One script just because of pure interest. So I put the Game and Menu car in one prefab (same for other 2) and add the all in one script function to each of UI buttons(I hope I explained well). Works perfectly! The only thing I have to fix about CarSelector system itself is that I can drive the selected cars while in garage, but that won’t be a problem to me. I guess I’m done with it (for now :slight_smile: ) Thank you so much Kelso I really appreciate your help!

Please Teach Me To Do :(:frowning: