0
\$\begingroup\$

I know how to deactivate a script that has the game object but I keep getting errors as I don't know how to deactivate a script that doesn't have a game object, presumably my script that "spawns objects". I put below a player script and a "spawner" script. Please advice? thank you so much

This is the player script

 private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Package")
        {
            Debug.Log("Got the Package");
            hasPackage = true;
            collision.gameObject.GetComponent<FallingPackage>().enabled = false;
        }

        if(collision.tag == "Customer" && hasPackage)
        {
            Debug.Log("Delivered");
            hasPackage = false;
        }
    }

This is the spawner script that spawns a game object from another script

 void Update()
    {
        if (Time.time > nextSpawnTime)
        {
            float secondsBtwnSpawn = Mathf.Lerp(secondsBtwnSpawns.y, secondsBtwnSpawns.x, Difficulty.GetDifficultyPercent());
            nextSpawnTime = Time.time + secondsBtwnSpawn;

            float spawnAngle = Random.Range(-spawnAngleMax, spawnAngleMax);
            float spawnSize = Random.Range(spawnAngleMin.x, spawnAngleMin.y);
            Vector2 spawnPos = new Vector2(Random.Range(-screenSizeUnit.x, screenSizeUnit.x), screenSizeUnit.y);
            GameObject newBlock = (GameObject)Instantiate(spawnPackage, spawnPos, Quaternion.Euler(Vector3.forward * spawnAngle));
            newBlock.transform.localScale = Vector2.one * spawnSize;
        }
    }
\$\endgroup\$
1
  • \$\begingroup\$ What do you mean by a script that doesn't have a game object? The two snippets you've shown us both look like MonoBehavíour methods, and Unity instructs you to only ever attach MonoBehavíour scripts to GameObjects — it will throw an error if you try to construct one "free floating". Please edit your question to show us a Minimal Complete Verifiable Example of the problem — the smallest complete set of scripts (not just snippets of one method, but the whole class) and setup instructions that we can use to reproduce this problem in a new, empty project. \$\endgroup\$
    – DMGregory
    Dec 28 '21 at 16:56
0
\$\begingroup\$

If you create an empty object in the editor, by right-clicking in the hierarchy and clicking Create Empty

enter image description here

(as shown in the picture above), and put the script in there it should work, as Unity requires that a script be put in a GameObject .

\$\endgroup\$
0
\$\begingroup\$

My wild guess is that you deleted a script which was on a GameObject or a prefab, and that's why Unity is throwing an error that the script is missing.

This could help: an editor script to find such missing references https://web.archive.org/web/20200724123545/http://wiki.unity3d.com/index.php/FindMissingScripts

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .