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

Cannot Add script components to objects

Discussion in 'Scripting' started by InsensitveJ0ker, Mar 19, 2020.

  1. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Hi, for some reason all of my objects with scripts give error saying that there is an error to the code.
    upload_2020-3-18_22-51-16.png
    And because of this I'm not too sure what is causing this. I've checked high and low for any syntax error. For example take the script "Spawn" in the picture above. All it's supposed to do is to spawn in a grid of objects. And there are no syntax errors.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawn : MonoBehaviour
    6. {
    7.     public GameObject blankSpace;
    8.     public Transform spawner;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.         for (int i = 0; i < 30; i++)
    15.         {
    16.             Vector3 offsetz = new Vector3(0f, 0f, 1.5f);
    17.             for (int j = 0; j < 20; j++)
    18.             {
    19.                 Vector3 offsetx = new Vector3(1.5f, 0f, 0f);
    20.                 Instantiate(blankSpace, spawner.position + (offsetz * i)+(offsetx * j), spawner.rotation);
    21.             }
    22.            
    23.         }
    24.     }
    I'm not sure what's causing this problem.
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Nothing in the editor console?
    This error does not necessarily refer to the script you are looking at. The scripts can not be loaded because compiling fails somewhere (else).
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    If the Spawn.cs script is posted in its entirety, it certainly does NOT compile the way it is above. You are (at least) missing a closing brace to end the class. If you're not seeing any errors, check that the red "Error" mark is enabled in the upper right corner of the console window.
     
  4. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Sorry, I had an empty method below that I didn't include because it, in theory, wasn't affecting the code. That's why you didn't see a closing bracket at the end.
     
  5. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    I checked with all the other scripts, and they don't seem to have any syntax errors.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    The fact that you "checked with all the other scripts" suggests to me that you didn't just look in the Unity Console, which is where you'd see any compile errors you would have. (Click on the bottom bar of the editor window, it will open the console)