Instantiate GameOBject with changing size in relation to another GameObject in the scene C#
Hello
I'm pretty new to programming and I don't think what I want to do is that hard but I somehow can't find an answer that works for me anywhere.
So I'm programming a spawner, and it works well this far, but there is one feature I fail to code. In my game, the Player's size is variable, it can grow and shrink (upon colliding with enemies). Because always spawning the same prefab woulnd't do, I want the size of my prefabs to be related to my player's size, regarding the fact that my player's size will often change.
More accurately, I don't want the spawned prefab to have the player's size, but something like randomly +/-2 the player's size. My size is an int and the player grows/shrinks of 1 when he collides (+1 if the the player is bigger than the enemy, and -1 if the enemy is bigger).
I'm working in 2D
Here is my Spawner :
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class SpawnTest : MonoBehaviour {
public int enemyCount;
public static int enemyCountstatic;//enemies (count)
public GameObject prefab;
private PlayerBehaviour _playerBehaviour;
int _playerSize;
private EnemyBehaviour _EnemyBehaviour;
float _enemySpeed;
public float chronostart;//timer to spawn
public float chrono;
public int maxEnemybase;//enemies in scene
public int maxEnemyallowed;
//test instantiate / avatar size
int _objectSize;
int _enemySize;
// Use this for initialization
void Start ()
{
enemyCountstatic = enemyCount;
_playerBehaviour = GameObject.Find("Player").GetComponent<PlayerBehaviour>();
_EnemyBehaviour = this.GetComponent<EnemyBehaviour>();
}
// Update is called once per frame
void Update ()
{
//determine size instantiate
_objectSize = _playerSize +- Random.Range(0, 2);
_playerSize = _playerBehaviour.playerSize;
//_enemySize = this.GetComponent<EnemyBehaviour>().enemySize;
maxEnemyallowed = maxEnemybase + _playerSize;
if (enemyCount < maxEnemyallowed)
{
chrono -= Time.deltaTime;
if (chrono <= 0)
{
prefab.transform.localScale = Vector2.one * _objectSize;
Instantiate(prefab);
chrono = chronostart;
enemyCount += 1;
}
}
}
}
So I already tried something with _objectSize = _playerSize +- Random.Range(0, 2); and the likes but it doesn't seem to work, the size doesn't change. So, the prefab is instantiated on its awake and its size doesn't change yet (It will in the future, I will add a way for the player to attack with projectiles rather than directly charge at the enemies, but enemies' size will only shrink and not grow). For gameplay reasons, I need to keep my size an int.
I hope I made sense. Thanks for your help !
try to switch these two lines around :
_objectSize = _playerSize +- Random.Range(0, 2);
_playerSize = _playerBehaviour.playerSize;