Search Unity

Pass Type In MonoBehaviour From Other Script

Discussion in 'Scripting' started by AnArrowInAKnee, Feb 20, 2021.

  1. AnArrowInAKnee

    AnArrowInAKnee

    Joined:
    Feb 20, 2021
    Posts:
    2
    So, here is my problem. ShootRaycastShotgun,as well as ShootRaycastBullet, method requires two generic parameters , one of them is enum. However, i know no way to store two variables of these types in PlayerShootWithRaycasts class(ShootRaycastShotgun/ShootRaycastBullet is called in this class' update). What is important, i can not predict what these variable types will be , 'cos it depends on user's choice.
    The question is : Is there any way for user to pass these two types in PlayerShootWithRaycasts?
    Please, correct me if i misuse some of these programmer terms.

    Here is PlayerShootWithRaycasts code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.Events;
    5.  
    6. public class PlayerShootWithRaycasts : MonoBehaviour, IUseExplosion
    7. {
    8.     private void Update(){
    9.     ///
    10.     /// if(...)
    11.     ShootingBasedOnShootMode();
    12.     ///
    13. }
    14.     private void ShootingBasedOnShootMode()
    15.     {
    16.         if(currentBulletCount > 0 && !isReloading)
    17.         {
    18.             bool shotSuccessful = false;
    19.             List<Vector2> hitPoints = new List<Vector2>();
    20.             if (currentGunMode == FightingAndShooting.CurrentGunMode.SingleShot)
    21.             {
    22.                 Vector2 hitPoint = FightingAndShooting.ShootRaycastBullet(muzzlePoint, targetPosition, bulletFlightDistance, damage, damagePerUnitReduction, forceToAddOnCollision, forcePerUnitReduction, targets);
    23.                 hitPoints.Add(hitPoint);
    24.                 shotSuccessful = hitPoint != Vector2.zero;
    25.             }
    26.             else
    27.             {
    28.                 hitPoints = FightingAndShooting.ShootRaycastShotgun(muzzlePoint, targetPosition, numberOfShotgunBulletsInShell, shotgunBulletsSpreadWidth, bulletFlightDistance, damage, damagePerUnitReduction, forceToAddOnCollision, forcePerUnitReduction, targets, obstacles);
    29.                 shotSuccessful = hitPoints != null;
    30.             }
    31.             if (shotSuccessful)
    32.             {
    33.                 ////////....
    34.                 }
    35.             }
    36.         }
    37.     }
    38. }
    39.  
    Here is ShootRaycastShotgun method :
    Code (CSharp):
    1. public static List<Vector2> ShootRaycastShotgun<T,T1>(Vector2 muzzlePoint, Vector2 targetPos, int numberOfBulletsInShell, float spreadWidth,  float bulletFlightDistance, float damage, float damagePerUnitReduction, float forceToAddOnCollision, float forcePerUnitReduction, LayerMask targets, T damageInfoEnum, T1 damageInfo) where T : System.Enum
    2.     {
    3.         if (Physics2D.OverlapPoint(muzzlePoint) == null)
    4.         {
    5.             List<Vector2> hitPoints = new List<Vector2>();
    6.             PointCalculation pointCalculation = new PointCalculation(targetPos, muzzlePoint, 1, spreadWidth);
    7.             Vector2 topBorder = pointCalculation.topBorder;
    8.             Vector2 bottomBorder = pointCalculation.bottomBorder;
    9.             Vector2 lineDirection = topBorder - bottomBorder;
    10.             for (int i = 0; i < numberOfBulletsInShell; i++)
    11.             {
    12.                 float distance = Random.Range(0, spreadWidth);
    13.                 Vector2 randomPoint = (topBorder - lineDirection.normalized * distance);
    14.                 Vector2 hitPoint = ShootRaycastBullet(muzzlePoint, randomPoint, bulletFlightDistance, damage, damagePerUnitReduction, forceToAddOnCollision, forcePerUnitReduction,targets,damageInfoEnum,damageInfo);
    15.                 if(hitPoint != Vector2.zero)
    16.                 {
    17.                     hitPoints.Add(hitPoint);
    18.                 }
    19.             }
    20.             return hitPoints;
    21.         }
    22.         return null;
    23.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
  3. AnArrowInAKnee

    AnArrowInAKnee

    Joined:
    Feb 20, 2021
    Posts:
    2
    Can you , please, specify, where i need to use the interface you mean? Because i am already using one. It is not seen in the code , but these two generic parameters are also required by Damage<T,T1>(T damageInfoEnum, T1 damageInfo) of IDamagable interface. So ShootRaycastBullet gets component of type IDamagable of hit object and calls Damage () on it. For example, if user wants to inform some object that it was hit by magic projectile, he will pass "MagicProjectile" to damageInfo and then hit object will use this information according to Damage() implementation in script, that derive from IDamagable. However, script can't know what user wants to tell object. That's why i am using generics. Therefore, i need some way to let user tell PlayerShootWithRaycasts what he wants to tell hit object. And i don't know one.
     
    Last edited: Feb 22, 2021
  4. danirijal

    danirijal

    Joined:
    Feb 21, 2021
    Posts:
    1
    Hello,

    please help my project,
    I got a problem when the script that I created couldn't be added to the button with the error description "can't add script behaviour assemblyinfo.cs. the script needs to drive from monobehaviour!", What should I do?

    and here is the script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Btn_manager : MonoBehaviour , IUseExplosion
    7. {
    8.    public void LoadScene(string scenename)
    9.    {
    10.        SceneManager.LoadScene (scenename);
    11.    }
    12. }
    13.  
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Don't try adding that class to a GameObject. Not sure why you would even have an assemblyinfo.cs class honestly, but maybe it came in with a package.

    I haven't done much with generic interfaces and how all that shakes out with C# implementation rules, but there are probably some tutorials for that specific way of things. I would start with implementing a concrete one, perhaps Vector2 or Vector3 for T / T1, then see if you can genericize it.

    If you look in my proximity_buttons package the Two Player Grippable Physics demo has some interfaces such as
    IGrippable
    and
    IPushable
    :



    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/