• 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 TenKHoursDev · 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 programming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Dot Product not equals 1 0 Answers

Can’t keep the position 0 Answers

Character picks up two cubes simultaneously 0 Answers

How to properly make an object follow the edge of another object? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges