Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by yanis010 · Sep 13, 2014 at 10:45 AM · c#random.range

Random.Range always returns min value (didn't set seed)

using UnityEngine; using System.Collections;

public class PrefabSpawn : MonoBehaviour {

 public GameObject[] items;
 int X = Random.Range(0,2);

 void Awake()
 {
     Instantiate (items[X], new Vector3(0,0,0), Quaternion.identity);
 }
 
 void Update () {
 
 }

}

Here's my code, it's supposed to spawn a prefab from a table, but it always spawns the one stored in items[0]. I found similar questions, but it is a seed problem, and I don't think it can fit my problem cause I'm not setting seed.

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 MrAkroMenToS · Sep 13, 2014 at 10:50 AM 1
Share

$$anonymous$$ake sure that you fill the items array properly. Try this:

 void Awake()
 {
 Instantiate (items[Random.Range(0, 2)], new Vector3(0,0,0), Quaternion.identity);
 }

Or this:

 int X;
 void Awake()
 {
 X = Random.Range(0, 2);
 Instantiate (items[X], new Vector3(0,0,0), Quaternion.identity);
 }

I always declare / add the starting value to my variables in the first method, not in the head of the script. I hope it helped, if not let me know.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zharik86 · Sep 13, 2014 at 10:49 AM

Correct initialization your variable "X". Change code:

  public GameObject[] items;
  int X = 0;
 
  void Awake() {
   X = Random.Range(0,2); //Gets int value from 0 to 1
   Instantiate (items[X], new Vector3(0,0,0), Quaternion.identity);
  }

I hope that it wil help you.

Comment
Add comment · Show 4 · Share
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 MrAkroMenToS · Sep 13, 2014 at 10:55 AM 1
Share

I dont see the point to answer an answered question :) I guess you didn't see my comment. I wrote the same.

  • His int is not public. By default variables are private.

avatar image zharik86 · Sep 13, 2014 at 01:54 PM 1
Share

@$$anonymous$$rAkro$$anonymous$$enTos Yes, you are right. Variable - private. And we wrote our answers at the same time.

avatar image AlucardJay · Sep 13, 2014 at 02:26 PM 0
Share

Simultaneous posts happen all the time :)

Upvotes for everybody!

avatar image Owen-Reynolds · Sep 13, 2014 at 06:17 PM 0
Share

The important thing, which zharik was mostly explicit about, is that you can't "run stuff" during a global declaration. Can only assign constants.

Otherwise, init everything in Start. Why didn't int X=Random... come up as an error? Beats me. And sometimes, illegal stuff does run, since the compiler-writers were bored.

avatar image
0

Answer by kumarc123 · Apr 12, 2015 at 06:31 AM

Hi, as per Unity Random.Range for int, the function works with maxValue exclusive. That is unityMaxValue = yourMaxValue + 1

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to slow down random enemy spawn? 1 Answer

I can't get Unity to recognise Random.Range C# 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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