Search Unity

Please help! Cant add script behaviour AssemblyInfo.cs. The Script needs to derive from MonoBehavior

Discussion in 'Scripting' started by cekorinka, Jun 12, 2019.

  1. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlanetsAndBonus : MonoBehaviour
    6. {
    7.     // An array the planets prefab to be spawned.
    8.     public GameObject[] obj_Planets;
    9.     // How long between each planets spawn.
    10.     public float time_Planet_Spawn;
    11.     // The speed at which the Planets moves.
    12.     public float speed_Planets;
    13.     //Planets list
    14.     // we will use this list so that the planets do not repeat.
    15.     List<GameObject> planetsList = new List<GameObject>();
    16.  
    17.     private void Start()
    18.     {
    19.         // Start function PlanetsCreation as a coroutine.
    20.         StartCoroutine(PlanetsCreation());
    21.     }
    22.  
    23.     IEnumerator PlanetsCreation()
    24.     {
    25.         // Fill the list with planets
    26.         for (int i = 0; i < obj_Planets.Length; i++)
    27.         {
    28.             planetsList.Add(obj_Planets);
    29.         }
    30.         // wait 7 seconds after the game started...
    31.         yield return new WaitForSeconds(7);
    32.         //Create planets...
    33.         while (true)
    34.         {
    35.             // Select a random planet from the list.
    36.             int randomIndex = Random.Range(0, planetsList.Count);
    37.             // Create an instance of the planet, taking into account the limits of the player’s movement width
    38.             // The planet will be created above the camera's visibility
    39.             // The planets will move at an angle in the range of -25 to 25.
    40.             GameObject newPlanet = Instantiate(planetsList[randomIndex],
    41.                 new Vector2(Random.Range(PlayerMoving.instance.borders.minX, PlayerMoving.instance.borders.maxX),
    42.                 PlayerMoving.instance.borders.maxY * 1.7f),
    43.                 Quaternion.Euler(0, 0, Random.Range(-25, 25)));
    44.  
    45.             //Remove the selected planet from the list
    46.             planetsList.RemoveAt(randomIndex);
    47.             // if the list is empty, fill it again
    48.             if (planetsList.Count == 0)
    49.             {
    50.                 for (int i = 0; i < obj_Planets.Length; i++)
    51.                 {
    52.                     planetsList.Add(obj_Planets);
    53.                 }
    54.             }
    55.             // On the created planet we find the component MovingObjects and set the speed of movement
    56.             newPlanet.GetComponent<ObjMoving>().speed = speed_Planets;
    57.             // Every time_Planet_Spawn seconds
    58.             yield return new WaitForSeconds(time_Planet_Spawn);
    59.         }
    60.     }
    61. }
    62. Please wirte in detail!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Why are you trying to add AssemblyInfo.cs to an GameObject? It's not derived from a monobehavior, that is what Unity is telling you.

    The script above actually IS a monobehavior. If you can't add it to a GameObject then you likely have a compiler error. Check your console log window in Unity.