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
1
Question by Kramboja · May 09, 2014 at 03:02 PM · updatespawnobjectsstartrepeat

Spawning different objects

Hey i've got a problem with spawning randomly objects out of a prefab folder on my screen that is being replaced after it passed the player.

The names of the objects are: wall1, wall2, wall3.

This is the script I use for spawning in the update() but before i used the same in the start() and he don't want to change it in the update function.

 randomWall = Random.Range (1, 4);
 
 GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + randomWall) as GameObject;
 


The problem is that he only spawns one kind of objects and he doesn't change it

the meaning is that it becomes an endless runner that random generate some before thought situations after each other.

edit:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WallManager : MonoBehaviour 
 {
     public GameObject runnerRef;
     public float distanceToRecycle = 20;
     public int numberOfWalls = 4;
     public int movement = -5;
     public List<GameObject> wall = new List<GameObject>();
     
     static int randomWall = Random.Range(1,4);
 
     void Start () 
     {
 
 
 
 
         GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + randomWall) as GameObject;
         for (int i=0; i<numberOfWalls; i++) 
         {
             GameObject b = Instantiate (wallPrefab, Vector3.zero, Quaternion.identity) as GameObject;
             Vector3 tempPos = new Vector3();
             if(wall.Count>0)
             {
                 tempPos = wall[wall.Count-1].transform.position;
                 tempPos.x += b.transform.localScale.x*.5f*35;
             }
             b.transform.position = tempPos;
             b.transform.parent = gameObject.transform;
             b.transform.parent.localPosition = new Vector3(30,12,20);
             wall.Add(b);
         }
     }
     // Update is called once per frame
     void Update () {
 
         randomWall = Random.Range (1, 4);
         Debug.Log (randomWall);
 
         transform.Translate (new Vector2 (movement * Time.deltaTime, 0));
 
         if (wall [0].transform.position.x + distanceToRecycle < runnerRef.transform.position.x) 
         {
             GameObject b = wall[0];
             wall.Remove(b);
 
 
             GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + Random.value * 2 + 1) as GameObject;
 
 
             Vector3 tempPos = new Vector3();
             if(wall.Count>0)
             {
                 tempPos = wall[wall.Count-1].transform.position;
                 tempPos.x += b.transform.localScale.x*.5f*35;
             }
             b.transform.position = tempPos;
             b.transform.parent = gameObject.transform;
             //b.transform.parent.localPosition = new Vector3(30,12,20);
             wall.Add(b);
         }
     }
 }

alt text

screen shot 2014-05-10 at 00.55.40.png (113.4 kB)
Comment
Add comment · Show 3
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 robertbu · May 09, 2014 at 03:04 PM 0
Share

There's so little code here that we'd be guessing at the problem. Please post the rest of the script. Note that Random.Range(1,4) will only produce the values 1,2,3.

avatar image thornekey · May 09, 2014 at 03:10 PM 0
Share

yeah, as robertu stated its hard to tell with a snipet..

Not really sure about random range, but you could try

 ("Prefabs/Wall" + randomWall.ToString()) 
avatar image Kramboja · May 09, 2014 at 03:23 PM 0
Share

@ robertu yes i know that, i got also 3 walls i would like to use now.

and i just posted the whole script that should do in how far i was include the problem above

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 09, 2014 at 03:59 PM

I believe your problem is here:

  GameObject wallPrefab = Resources.Load ("Prefabs/Wall" + Random.value * 2 + 1) as GameObject;

Instead of using 'randomWall' as you do in Start(), you are using some other calculation. Note that Random.value will always be between 0 and 1.

Comment
Add comment · Show 5 · 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 Kramboja · May 09, 2014 at 04:58 PM 0
Share

well in the start() he gives it a value like wall2 and then in the update() it also don't change.

Also in my game itself i get 6 times wall2 then. I guess i have to change that and every time that the wall is far enough from the player it removes to put another random wall.

I only haven't really an ideal how i change the game object that it makes different ones.

avatar image robertbu · May 09, 2014 at 05:20 PM 0
Share

Do this on the line before the Instantiate() in Update():

 string st = "Prefabs/Wall" + Random.value * 2 + 1;
 Debug.Log(st);
avatar image Kramboja · May 09, 2014 at 08:01 PM 0
Share

this doesn't work.

I'm just sure there is first a problem in the start function why he doesn't say there 3 walls but the same wall all the time. but i don't know what i have to change to make the difference.

avatar image robertbu · May 09, 2014 at 08:47 PM 0
Share

What does 'this doesn't work' mean. I suggested these to lines of code to debug your problem...you could see what string was used and therefore what wall it was trying to get. So what did it say? And why are you not using Random.Range() like in Start()?

avatar image Kramboja · May 09, 2014 at 10:47 PM 0
Share
 public List<GameObject> wall = new List<GameObject>();

Is only using one type of the wall and in the update() it doesn't change no matter what what i want it to do. Also is there no difference in the start(), it makes 6 walls of the same number even when i tried to put the randomWall = Random.Range (1, 4); in the for loop of the start().

It only takes a single object and use it for all the time.

First i was using random.range also in the update() but it didn't work, also my $$anonymous$$cher and partner in making the game said i should use random.value

If i know the solution of the start() problem i will probably to fix the update() with the same problem.

maybe the screenshot i added will help

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

21 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

Related Questions

Does using both Update() and a coroutine cause respawning problems? 0 Answers

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

Failed to start unity package manager. Version 2017.2 1 Answer

Question about Unity documentation GameObject.Find 2 Answers

Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...? 2 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