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

Anyone able to Explain how to use a none monobehavior class

Discussion in '2D' started by Grengoshi, May 18, 2020.

  1. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    I have a SpawningManager and a none monobehehavior class called zombie. I have it so you can set the values of the Zombies inside spawningmanager i have adjusted it recently. inside the zombie entity i cant set its to 1 or 2 etc, but how do i actually get those values to set inside of of example movement method.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ZombieSpawnerManager : MonoBehaviour
    6. {
    7.     //start
    8.     private Transform player;
    9.     public float moveSpeed = 2.0f;
    10.     private Rigidbody2D rb;
    11.     private Vector2 movement;
    12.     private ZombieSpawnerManager _zSpawnManager;
    13.     public int deleteListValue;
    14.     private PlayerController pControllerVar;
    15.  
    16.     //stop
    17.  
    18.     public GameObject zombieOne;
    19.     public GameObject zombieTwo;
    20.  
    21.     private int _amount; //thee amount that gets spawned
    22.  
    23.     private int _value;
    24.     public int level;
    25.     public float state;
    26.     private int complexity;
    27.  
    28.     public int spawnValueCost;
    29.     public Vector2 fillerForNow;
    30.     public List<GameObject> spawnTracker = new List<GameObject>();
    31.     public LevelManager levelManager;
    32.     public int gameLevel;
    33.     private Zombie zombieClass;
    34.  
    35.     private bool zombie1;
    36.     private bool zombie2;
    37.  
    38.     private Zombie movementVariable;
    39.     private float speed;
    40.     private void Awake()
    41.     {
    42.         levelManager = FindObjectOfType<LevelManager>();
    43.         //start
    44.         //_zSpawnManager = FindObjectOfType<ZombieSpawnerManager>();
    45.         pControllerVar = FindObjectOfType<PlayerController>();
    46.         player = pControllerVar.GetComponent<Transform>();
    47.  
    48.         //stop
    49.         movementVariable.MovementSpeed.moveSpeed
    50.  
    51.  
    52.     }
    53.     private void Update()
    54.     {
    55.         Movement();
    56.     }
    57.  
    58.     public void SpawnZombie(int _amount) //will spawn the zombies on amount / value
    59.     {
    60.  
    61.         for (int i = 0; i < _amount; i++)
    62.         {
    63.             if (zombie1==true)
    64.             {
    65.                 Zombie zombieTypeOne = new Zombie(1, 1, 1, zombieOne, fillerForNow);
    66.                 zombieTypeOne.SpawnZombie();
    67.                 spawnTracker.Add(zombieOne);
    68.             }
    69.  
    70.             if (zombie2==true)
    71.             {
    72.                 Zombie zombieTypeTwo = new Zombie(2, 2, 2, zombieTwo, fillerForNow);
    73.                 zombieTypeTwo.SpawnZombie();
    74.                 spawnTracker.Add(zombieTwo);
    75.             }
    76.            
    77.          
    78.         }
    79.  
    80.        
    81.  
    82.     }
    83.  
    84.     public void SpawnZombieByComplexity(int level)//sets level on complexity based on the level of the scene
    85.     {
    86.  
    87.         //_remainderSystem = 1 % level;
    88.         //complexity = (level * _remainderSystem);
    89.         complexity = level;
    90.  
    91.         if (complexity >= 0)
    92.         {
    93.             zombie1 = true;
    94.         }
    95.  
    96.         if (complexity >= 2)
    97.         {
    98.             zombie2 = true;
    99.         }
    100.         _amount = 1 * level;
    101.         SpawnZombie(_amount);
    102.         //need to take complexity and compare it to the zombie comparative.
    103.         //calls the complexity method to spawn a specific amount of zombies
    104.  
    105.     }
    106.  
    107.     public void DesytroyZombieListCollection(int deleteListValue)
    108.     {
    109.         spawnTracker.Remove(zombieOne);
    110.         if (spawnTracker.Count == 0)
    111.         {
    112.             levelManager.LevelIncrementer();
    113.         }
    114.  
    115.     }
    116.  
    117.     //when the list is emprty we need to call the level
    118.  
    119.     private void Movement()
    120.     {
    121.  
    122.         Vector3 direction = player.position - transform.position;//finds direction of the player by subtracting player from enemy
    123.  
    124.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;//takes the direction and calculates the angle
    125.  
    126.         rb.rotation = angle;//rotates the rb of the enemy to the player
    127.         direction.Normalize();//0-1 on angle to keep smooth
    128.         movement = direction * speed; //makes the direction var into the movement value
    129.         Vector2 moveAdjust = new Vector2(movement.magnitude, 0);
    130.  
    131.         transform.Translate(moveAdjust * Time.deltaTime);
    132.     }
    133. }
    134.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Zombie
    6. {
    7.  
    8.     private float _xMin = 0f;
    9.     private float _xMax = 25f;
    10.  
    11.     private float _yMin = 0f;
    12.     private float _yMax = 25f;
    13.  
    14.     public int ZombieLife;
    15.     public float ZombieSpeed;
    16.     public int ZombieDamage;
    17.    
    18.     public GameObject ZombiePrefab;
    19.     public Vector2 ZombieSpawnTransform;
    20.  
    21.  
    22.     private ZombieSpawnerManager zombieSpawnManager;
    23.  
    24.     public float movementVariable;
    25.  
    26.     public Zombie(int zombieLife,float zombieSpeed, int zombieDamage, GameObject zombiePrefab,Vector2 zombieSpawnTranform)
    27.     {
    28.         ZombieLife = zombieLife;
    29.         ZombieSpeed = zombieSpeed;
    30.         ZombieDamage = zombieDamage;
    31.         ZombiePrefab = zombiePrefab;
    32.         ZombieSpawnTransform = zombieSpawnTranform;
    33.        
    34.     }
    35.    
    36.  
    37.  
    38.  
    39.     public virtual void MovementSpeed()
    40.     {
    41.         movementVariable = ZombieSpeed * 2;
    42.     }
    43.     public virtual void Attack()
    44.     {
    45.         // Get player health, - attackDamage;
    46.     }
    47.  
    48.     public virtual void Movement()
    49.     {
    50.         //changes the speed of each zombie thats presented
    51.  
    52.     }
    53.  
    54.     public virtual void ZombieSpawningVector()
    55.     {
    56.         float x = Random.Range(_xMin, _xMax);
    57.         float y = Random.Range(_yMin, _yMax);
    58.         ZombieSpawnTransform = new Vector2(x, y);
    59.  
    60.         GameObject.Instantiate(ZombiePrefab, ZombieSpawnTransform, Quaternion.identity);
    61.  
    62.     }
    63.  
    64.     public void SpawnZombie()
    65.     {
    66.         ZombieSpawningVector();
    67.     }
    68.  
    69.  
    70.  
    71.  
    72. }
    73.  
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Grengoshi

    Does this question have something to do with 2D graphics or is this a generic scripting question?

    "but how do i actually get those values to set inside of of example movement method."

    Not sure what the question exactly is or what you are trying to achieve here... but if you need to access zombies later, put them in array or list of type Zombie to access them without casting. You could also make Zombie class have serializable attribute to make it visible in inspector, if that is needed.

    Also, that Movement method in your ZombieSpawnerManager... not sure if you did use some example code or not, but you already seem to have empty Movement method in your Zombie, so simply place the move towards code there, and let zombie update itself? You could do this by either making Zombie a MonoBehaviour with Update method or call each Zombie in turn with its Movement method (I would also rename it to Move), something like this: myZombie.Move(), but either way, I wouldn't make a spawner class to update Zombies.
     
  3. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    I am sorry it was early my time and i was exhausted, also i maybe american but my english writen skills suck.

    and yes this is a 2d game so the properties are meant for 2d support.

    zombie is a class on its own it does not inherit from mono, However there is a method in it called movement speed.
    knowing this there is also a script called zombiemanager and it manages how zombies work inside the game, iof you look at the zombies inside the manager you will see number vales 1,1,1,predfab, spawningtranfrosm. these change the properties of the zombies in the zombie class. however I do not know i how change the properties from zombieone(1,1,1) in manager and apply them inside the class and then reout put them inside the manager, i dont know if this helps clarify my question.