Unity "Can't add script" Error Resolution

Asked 2 years ago, Updated 2 years ago, 155 views

Unity Visual Studio Programming Questions

I am making a 2D shooting game by referring to the website below, but when I try to put the source code below into the player in Asset and Prehab, there is an error and I cannot put it in.
I'm sorry to be selfish, but I'm a beginner in both Unity and C# languages, so please tell me the solution with few technical words.

I understand that Visual Studio says you need to mess with something, but I'm just a beginner and just follow the website, so I don't know.

[Unity] Create a 2D Shooting Game 2 [Tutorial]

error message

Can't add script behavior AssemblyInfo.cs.The script needs to derivative MonoBehavior

src=

source code

Player.cs
using UnityEngine;
using System.Collections;

namespace CompletedAsets
{
    public class player —MonoBehavior
    {
        // Spaceship component
        Spaceship;

        IEnumerator Start()
        {
            // Retrieve Spaceship components
            spaceship=GetComponent<Spaceship>();;

            while(true){

                // Create bullets at the same position/angle as the player
                spaceship.Shot(transform);

                // make a shot sound
                GetComponent<AudioSource>().Play();

                // wait for shotDelay seconds
                yield return new WaitForSeconds (spaceship.shotDelay);
            }
        }

        // Speed of movement
        public float speed = 5;


        void Update()
        {
            // right and left
            float x = Input.GetAxisRaw("Horizontal");

            // top and bottom
            floaty=Input.GetAxisRaw("Vertical");

            // seek the direction of movement
            Vector2direction = new Vector2(x,y).normalized;

            // Replace the direction and speed of movement
            GetComponent<Rigidbody2D>().velocity=direction*speed;

            // restriction of movement
            Move (direction:direction);

        }

        // movement of the airframe
        void Move (Vector 2 direction)
        {
            // Get the world coordinates at the bottom left of the screen from the viewport
            Vector2min = Camera.main.ViewportToWorldPoint (new Vector2(0,0));

            // Get the world coordinates in the upper right corner of the screen from the viewport
            Vector2max = Camera.main.ViewportToWorldPoint (new Vector2(1,1));

            // Get player coordinates
            Vector2pos = transform.position;

            // add the amount of movement of
            pos+=direction*spaceship.speed*Time.deltaTime;

            // limit the player's position to fit on the screen
            pos.x = Mathf.Clamp(pos.x, min.x, max.x);
            pos.y = Mathf.Clamp(pos.y, min.y, max.y);

            // place a restricted value at the player's position
            transform.position=pos;
        }

        // be called the moment someone hits
        void OnTriggerEnter 2D (Collider 2Dc)
        {
            // Get layer name
            string layerName = LayerMask.LayerToName(c.gameObject.layer);

            // Delete bullet when layer name is Bullet (Enemy)
            if(layerName=="Bullet(Enemy)"){
                // removal of a bullet removal
                Destroy(c.gameObject);
            }

            // Explosion if layer name is Bullet (Enemy) or Enemy
            if(layerName=="Bullet(Enemy)"||layerName=="Enemy"){
                // Find and retrieve Manager components from within the scene and call GameOver method
                FindObjectOfType<Manager>().GameOver();

                // explode
                spaceship.Exploration();

                // Delete Player
                Destroy(gameObject);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals (object other)
        {
            return base.Equals(other);
        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}

c# unity2d

2022-09-30 21:44

1 Answers

This problem is not caused by the script.

Do the following:

  • Copy the script to Notepad
  • Delete the script
  • Close Visual Studio
  • Create a new script from the Unity Editor (exactly the same name as before)
  • Double-click the script from the Unity Editor to open it.
  • Paste the notepad code internally and save it

If not, please refer to the other answers.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.