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

Zombie Toys- Cannot add attack scripts to player entitiy

Discussion in 'Scripting' started by sum1nil, Mar 6, 2020.

  1. sum1nil

    sum1nil

    Joined:
    Oct 28, 2017
    Posts:
    4
    I have a copy of Zombie Toys. This includes the attack scripts that one should be able to 'drag and drop' upon the player component. This is the initial part of the 'Player Attack' script.

    Code (CSharp):
    1. //This script handles the player's ability to attack. The biggest //responcibility of this script is to maintain the timing of //attack cooldowns
    2. //so that the player cannot attack too fast. Mostly, this is a
    3. //"pass through" or "bridge" //script, which means that it receives input from the
    4. // PlayerInput scripts and then passes the input along to the appropriate attack.
    5. //Very little attack logic (apart from timing) exists in this
    6. script.
    7. using UnityEngine;
    8. public class PlayerAttack : MonoBehaviour
    9. {
    10.     [Header("Attacks")]
    11.     [SerializeField] LightningAttack lightningAttack;   //Reference to a lightning attack script
    12.     [SerializeField] FrostAttack frostAttack;            //Reference to a frost attack script
    13.     [SerializeField] StinkAttack stinkAttack;            //Reference to a stink attack script
    14.     [SerializeField] SlimeAttack slimeAttack;            //Reference to a slime attack script
    15.     [SerializeField] int numberOfAttacks;                //The number of attacks the player has
    16. ...
    This is an example of an Attack script.
    Code (CSharp):
    1. public class LightningAttack : MonoBehaviour
    2. {
    3.     [Header("Weapon Specs")]
    4.     public float Cooldown = 1f;                     //The cooldown of the attach
    5.     [SerializeField] int damage = 20;                 //How much damage the attack does
    6.     [SerializeField] float range = 20.0f;            //How far the attack can shoot
    7.     [SerializeField] LayerMask strikeableMask;         //Layermask that determines what the attack can hit
    8. ...
    There are 4 slots to drop an attack but I cannot drag and drop any onto them. It seems to me that since the player attack script has these classes, e.g. LightingAttack, that those scripts should drop onto the Player Attack component which has statements like
    [SerializeField] LightningAttack lightningAttack;
    .
    Further there are other scripts with fields calling for classes that do not seem to want to accept even though there are fields for them. Whenever I try to drag the attack scripts to the field - all I get is a circular buster symbol and it is unwilling to accept. Has anyone had this problem. My file is originally from Github and Unity converted it to Unity 19.3. Perhaps there is a locking mechanism Unity is using to prevent this behavior or problems with the conversion.
    I would appreciate any clarification of this dilemma. Thank you.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you trying to drag the script from your Assets folder itself? Because MonoBehaviour scripts are components which attach to GameObjects. You would drag an attached instance of the script to the field, or even the GameObject itself it is attached to.
     
    sum1nil likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    To clarify what Uncle Joe wrote above (which is spot on), the comments in the first snippet of posted code are misleading.

    You are not dragging the SCRIPT in there, you're dragging a valid instance of a script into those fields, which means what Uncle Joe said: it has to be properly added already to a GameObject, which is then what you would drag in there.
     
    Joe-Censored and sum1nil like this.
  4. sum1nil

    sum1nil

    Joined:
    Oct 28, 2017
    Posts:
    4
    Thank you for the clarification. I made a game object and attached an attack script. It was accepted no problem. Thank you again.
     
    Kurt-Dekker and Joe-Censored like this.