Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by yesa · Jan 10, 2016 at 05:50 AM · c#2drandomsizeinstantiate prefab

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 !

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vintar · Jan 10, 2016 at 06:41 AM 0
Share

try to switch these two lines around :

_objectSize = _playerSize +- Random.Range(0, 2);

  _playerSize = _playerBehaviour.playerSize;

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Stepping on game object moves quickly to react 0 Answers

Attemped to use GameObject.GetComponent().bounds.size.y to find the height of a sprite, but weird glitch happening when I do. 1 Answer

Display pictures in assets folder in random order 0 Answers

Changing Player Position when hit by collider 0 Answers

Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges