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!
     
    Last edited: Jun 12, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
    palex-nx I did. Can you solve my problem?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You haven't really described your problem. The error suggest you're trying to add a script to a gameobject and since that script doesn't inherit from monobehaviour, it can't be added.

    So what are you trying to do that creates that error?
     
  5. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
    Does that show my problem?
     

    Attached Files:

  6. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
    This script should change the planets in random order and even to make them move
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I see there is at least one script compilation error in console. Fix all such errors and try again, it should work.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    So you're trying to access a script "ObjMoving" but if you look at your actual script name, it appears to be ObjectMoving.

    It's important that script name matches the class name and that you try to access it through that name.
     
  9. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
     
  10. cekorinka

    cekorinka

    Joined:
    Jun 12, 2019
    Posts:
    13
    Большое спасибо)))
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    pozhalujsta