Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 heyhujiao · Dec 16, 2015 at 01:23 PM · c#2dtransformpositionvector3

Trying to Generate Different Random Values for Position of Game Object Instances [C#]

I am trying to generate different random values for the position of my game object whenever it is re-instantiated. For some reasons, it is not working. Can someone help to see what is wrong? (newPosition is the value that I hope it would be different everytime a new gameobject is generated.)

 using UnityEngine;
 using System.Collections;
 
 public class KillObjectGenerator : MonoBehaviour
 {
 
     public float randomTimeDelay;
     private Vector3 newPosition;
     public float spawnTime = 3.0f;
     public GameObject cloud;
     public GameObject lightning;
     private float randomX = Random.Range(-8.3f, 0.78f);

     void Start()
     {
         randomTimeDelay = Random.Range(3f, 6f);
 
         // Start calling the Spawn function repeatedly after a delay.
         InvokeRepeating("Generator", randomTimeDelay, spawnTime);
     }
 
     void spawnNewPosition()
     {
         newPosition = new Vector3(randomX, 0.18f, 0);
     }
 
     void Generator()
     {
         Instantiate(cloud, newPosition, Quaternion.identity);
         Instantiate(lightning, newPosition, Quaternion.identity);
         Debug.Log("Spawned a kill object!");
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Dec 16, 2015 at 07:14 PM

randomX is assigned a random value once during construction. You want a random value for every new spawn position. So why not move the Random assignment to inside of the spawnNewPosition() function?

 void spawnNewPosition()
 {
     randomX = Random.Range(-8.3f, 0.78f);
     newPosition = new Vector3(randomX, 0.18f, 0);
 }
Comment
Add comment · Show 3 · 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 heyhujiao · Dec 17, 2015 at 02:27 AM 0
Share

But I can't put a function inside

Instantiate(cloud, spawnNewPosition(), Quaternion.identity);

because there will be error trying to put a function inside a field that is for vector3.

avatar image Jessespike heyhujiao · Dec 17, 2015 at 03:59 PM 0
Share

Then don't put the function inside, call it before:

 void Generator()
 {
     spawnNewPosition();
     Instantiate(cloud, newPosition, Quaternion.identity);
avatar image TonicMind · Dec 17, 2015 at 04:23 PM 0
Share

You could also just call SpawnNewPosition() just before you instantiate your cloud and lightning gameobjects. Whats key here is the order of which you do things. In a script or any program$$anonymous$$g language the flow of reading and execution is always Top to bottom and left to right, ignoring any operator precedence. You created that function spawnnewposition() but you haven't called it anywhere. That's your problem. You should call it before the call to Generator(), or inside it before the value for newposition is used in the instantiate function. Functions CAN call other functions, in fact invoke(), and Log() are functions themselves.

With the exception of constructors (which is also a kind of function) anything with () following it is a function!

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

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with this code? thanks. 2 Answers

Placing GameObjects in a circle in scene view 1 Answer

Are I stupid? Vector3 1 Answer

Dot Product not equals 1 0 Answers

why when putting a cube prefab as a child of another moving object it change the cubes scale !!! 0 Answers


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