Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Can't reference Function parameters in Foreign Script

Discussion in 'Scripting' started by DrollerDuck, Dec 18, 2019.

  1. DrollerDuck

    DrollerDuck

    Joined:
    Sep 21, 2019
    Posts:
    4
    My Play Animation Script won't accept The parameter from a function from my entity script.

    I am trying to make it so an animation plays when a entity takes damage
    All the damage is taken care of in two other scripts one an Entity script which has all the Entity stats like Health, Defense ect.
    It also houses a void that receives damage values from foreign scripts like my sword script in the Int Parameter brackets which are referenced in the sword scripts and applied in the Entity script.
    I cannot reference the Int parameter from the Play Animation Script to add to the update void.
    _Damage won't work like i want it to, i can't really explain it because i'm not very literate in C#
    the Error message is as follows:
    Code (CSharp):
    1. Invalid token '=' in class, struct, or interface member declaration [Assembly-CSharp, Assembly-CSharp]
    2. Invalid token ';' in class, struct, or interface member declaration [Assembly-CSharp, Assembly-CSharp]
    3. The name '_Damage' does not exist in the current context [Assembly-CSharp]
    4. The name 'damage' does not exist in the current context [Assembly-CSharp]
    5. The name '_Damage' does not exist in the current context [Assembly-CSharp]
    I don't tell it very vividly so here is the Entire copy and pasted Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AttackedAnimations : MonoBehaviour
    6. {
    7.        public Animator anim;
    8.        //Damageable ForeignDamage;
    9.        damage = _Damage;
    10.        public void start()
    11.        {
    12.            _Damage = gameObject.GetComponent<Damageable>().TakeDamage(damage);
    13.        }
    14.     void Update()
    15.     {
    16.         if(GetComponent<Damageable>().TakeDamage(_Damage))
    17.         {
    18.             anim.Play("EnemyDamaged");
    19.         }
    20.     }
    21. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,501
    Line 9 is invalid. You are just saying
    damage = _Damage;
    at the script member level, which does not mean anything.

    What type of variable do you want to declare as
    damage
    ? Where is the
    _Damage
    quantity coming from to assign to it? If you are doing it there in class member definition scope, then
    _Damage
    must be a constant. Otherwise you must do it in a function.

    ALSO: be very careful with capitalization. You have a function called
    start()
    and that will NOT be called by Unity. Unity calls
    Start()
    in a very specific point in time in the lifecycle of a Monobehavior instance.

    You may wish to slow down and back up to some basic scripting and language tutorials to understand how to express what you are trying to do.
     
unityunity