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 Mightycam · Nov 23, 2014 at 01:28 AM · gameobjectarrayvector3quaternionrandom

C# Array with random.range help?

Hey guys I'm trying to get this random.range stuff to work where the scene is a car race, and you got to collide into a cube and one of two game objects appears. I want to make it so that both objects appear randomly when you collide with the cube. Like hit one cube it will be a yellow one hit the next one and it will be red, then hit the third one and it might be red again?

using UnityEngine; using System.Collections;

public class PowerUp : MonoBehaviour {

 public GameObject[] powerupObject;

 void OnTriggerEnter(Collider collision)
 {
     GameObject newPowerup;
     newPowerup = Instantiate(powerupObject [0], this.transform.position, Quaternion.identity)as GameObject;
     Vector3 temp = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
     temp.y = 5;
     temp.z = this.transform.position.z + 10;
     newPowerup.transform.position = temp;
     Destroy (this.gameObject);
     

 }

}

Comment
Add comment · Show 6
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 MrSoad · Nov 23, 2014 at 12:53 AM 0
Share

If you have two items in your object array then you have two options, 0 and 1, in terms of the array index. So use random range to pick either 0 or 1, then use this value in your object instantiate.

avatar image Mightycam · Nov 23, 2014 at 01:03 AM 0
Share

can you help me write it out. I'm sort of new to c# scripting and still learning arrays at the moment?

avatar image MrSoad · Nov 23, 2014 at 01:08 AM 1
Share

Take a look at these parts of the documentation for a very good explanation as to how these work(If you still are not sure after reading these than have a good Google).

http://docs.unity3d.com/ScriptReference/Random.Range.html

http://docs.unity3d.com/ScriptReference/Array.html

It is not that hard(a very very small amount of code to add to the script you have found) and it is a good opportunity for you to learn, if I write it out for you then you will learn nothing and you will have to ask us to write it out for you again the next time, and the next until you do :)

avatar image Mightycam · Nov 23, 2014 at 01:51 AM 0
Share

okay I'm sort of stuck on this error.This is what I have changed so far. Can you just sort of give me a run through on this error? I've looked around for this error but everyones example and how they have written the script is far different.

 Vector3 temp = Random.Range(newPowerup.transform.position);

and the error I get is PowerUp.cs(13,39): error CS1501: No overload for method Range' takes 1' arguments

avatar image MrSoad · Nov 23, 2014 at 02:01 AM 0
Share

Create an int variable and assign the random range(which will be 0 or 1) to this int variable.

Now use this int variable in your :

 newPowerup = Instantiate(powerupObject [0], this.transform.position, Quaternion.identity)as GameObject;

line ins$$anonymous$$d of the 0.

 newPowerup = Instantiate(powerupObject [USE_IT_HERE], this.transform.position, Quaternion.identity)as GameObject;
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RadioactiveTechnologies · Nov 23, 2014 at 04:51 AM

Random.Range() takes 2 INTEGERS not FLOATS. Try that and see if it helps..

Comment
Add comment · Show 1 · 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 Mightycam · Nov 23, 2014 at 05:02 AM 0
Share

Still trying my best man. I've only been doing this for 5 weeks with only 3 hours of class time so I'm struggling here.

avatar image
0

Answer by Jignesh G. · Nov 23, 2014 at 06:52 PM

Below code should work properly.

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public GameObject prefab;
     void Start() {
         Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
         Instantiate(prefab, position, Quaternion.identity) as GameObject;
     }
 }


and for your kind knowledge random.range accepts both integer and floats.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Linear interpolating (Lerping) a Gameobject between random positions of Other GameObjects 1 Answer

Random.insideUnitCircle seems to reproduce similar vectors 1 Answer

Vector3 Position returns 0 when applied to GameObject 1 Answer

How do I make create a rotateable object that follows a raycast hit point? 2 Answers

Spawn object using vector array anywhere but previous spawn location 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