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 Rick.Miranda · Sep 15, 2013 at 05:41 PM · instantiatespawningscrolling

Help with Generating/Spawning Background Object

Hi

Up to this point, I've been generating an object with texture and using that as my background image. I start off with two objects, 1.33 wide by 2.66 tall (each). These have also been made into prefabs called "Platform." They're called platform but they're really background objects. They are stacked vertically and scroll downwards. When the first object's top edge reaches the end of the camera view, it generates a new background object, above the second object (that was there at the beginning) so that it creates a continuous scrolling background (as far as the camera can tell). This works great if you're only using one prefab with the same texture. The texture is a top down view of some landscape. But now, I want to be able to generate a changing landscape. I want to randomly determine if the landscape will transition and if so, I want to spawn another prefab with a different landscape texture. So to start, I created a separate background object with a different texture.

Before this, I was using transform.position = new Vector 3( ) to generate the new prefab but since now I want to access other prefabs in the assets folder, I'm trying to use "Instantiate( )". When I use new Vector 3( ), the prefabs generate only when the condition is met and works great. But now, when I use "Instantiate ( )", the prefab is generated almost back to back, instead of only when the prefab (self) reaches the final position.

Maybe I'm missing something about "Instantiate ( )" that I don't understand or not sure what the solution for this problem is here. Any help would be great...

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 supernat · Sep 15, 2013 at 07:03 PM 0
Share

I'm not sure I follow what you mean when you say you generated a new prefab using transform.position = new Vector3(). That line of code only changes the position of a game object. Are you saying that you had 2 game objects in the scene in the editor and you basically just reposition the lowest object once it moves offscreen, but now you want to dynamically create the object and move it ins$$anonymous$$d?

Please provide code sample and a description of your design. I don't know where you're calling Instantiate from, what code is moving the objects (do they move themselves?), etc.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by supernat · Sep 16, 2013 at 01:06 AM

Oh I think I see. You need to destroy the game object that creates the new prefab instance. Otherwise, that game object will continue to move down the screen, its position will continue to be < -1.55, and it will keep spawning new ones. Just add a Destroy(gameObject) right after you instantiate the new one.

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 Rick.Miranda · Sep 16, 2013 at 01:38 AM 0
Share

Ok :-) That did the trick. I wasn't used to implementing that since, with new Vector 3 ( ), you don't need to destroy the object.

Thank you :-)

One question though, I'm having an issue with instantiating the default background, when transition_Bool is False. I get the following error message: "ArgumentException: The Prefab you want to instantiate is null." This happens for the prefab path "Assets/CityStreets/Prefabs/Plaform.prefab". Since this Prefab is "null", it instantiates the other one ins$$anonymous$$d, "Platform_Bridge."

avatar image supernat · Sep 16, 2013 at 02:00 AM 0
Share

Hmmm...misspelling maybe? Plaform ins$$anonymous$$d of Platform?

avatar image Rick.Miranda · Sep 16, 2013 at 02:14 AM 0
Share

Yeah, that was it :-) You've saved me hours of frustration. Thanks again.

avatar image
0

Answer by Rick.Miranda · Sep 16, 2013 at 12:12 AM

Hi Supernat, thanks for trying to look into this.

Regarding transform.position = new Vector3( ), you are correct. I was initially using this to make the background object change it's position, once it reached a certain final position. This was implemented only on one default background object.

Now, what I want to do is, dynamically select from different prefabs (same cube object with same dimensions each, only difference is that each object will have a different texture), spawn the prefab in the position desired, and it will scroll from top to bottom.

As far as scrolling, I'm using the Playmaker Plugin. The object has a Playmaker component attached that has a Finite State Machine. Once the object is spawned, it scrolls the object from top to bottom. So, we don't need to worry about the scrolling portion of this, that works the way it's supposed to, even when I use Instantiate ( ). The problem with Instantiate is that somehow, instead of doing the action when the object (self) is at the terminal position, it's spawning repeatedly.

The script attached to the object does a couple of things: (1) checks the current position of the objet it's attached to (2) once object reaches terminal position, repositions self at the new desired position (using new Vector 3 ( ) ).

I want to change the script around to where it (1) checks the position (2) spawns a prefab (with disctinct texture attached) saved in the assets/prefabs folder. Here is the code I was using up to now, this is the one that simply takes the default background and repositions it using new Vector 3 ( ):

pragma strict

var speed : float; var position_y : float;

function Update () {

      //The game starts off with 2 tiles that are 2.66 units long each (5.32 units total)
      //The new position of the first background object (position_y) = (2.66x2)-1.55=3.77 units
      //This ensures the background object is placed right above (edge to edge) the second object
      if (transform.position.y <= -1.55) {
          position_y = 3.77f 
          transform.position = new Vector3(0, position_y, 1);
      }

}

When using a single background object this works fine. But now, I want to create several prefabs. I'm only using two prefabs right now to test with until I can get this down then I will create more prefabs. But I want the script attached to the background object to (1) check it's current position (2) if at the terminal position then, spawn one of two prefabs in the prefabs folder at the desired position. The prefab will already have the scroll FSM attached and will scroll once spawned. (3) Don't do anything except scroll until it reaches terminal position then, spawn another prefab at the desired position.

Here is the script I'm implementing. Again, keep in mind that scrolling works fine. The problem I'm having is that with this script, once the object instantiates, seems to be instantiating prefabs one after the other (one clone each frame or so) even as each one scrolls down the way it's supposed to. Here's the code I'm using:

 function Update () {
      if (transform.position.y <= -1.55) {
      position_y = 3.77f;
      transition_Bool = (Random.value > 0.5f);
      //Each if statement will set the Prefab Path (string) for the specific prefab to be spawned
             if (transition_Bool == true) {
               Prefab_Path = "Assets/CityStreets/Prefabs/Platform_Bridge.prefab";
                }
                else if (transition_Bool  == false) {
                Prefab_Path = "Assets/CityStreets/Prefabs/Plaform.prefab";
                }
                else { 
                }
             Instantiate(AssetDatabase.LoadAssetAtPath(Prefab_Path, GameObject), Vector3(0, position_y, 1), Quaternion.identity);
         }
 }

This is the same basic structure as the first code section. Only difference is the if statement for setting the prefab path, but that only sets the prefab path for the Instantiate function to use. The only other difference here is that instead of using new Vector 3, I'm using Instantiate ( ).

Not sure what it is about this second script that's causing the prefab to spawn what seems to be every frame, instead of only once, after the lower edge of the last object reaches the terminal position.

Any help will be great.

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

16 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

Related Questions

Gap Between Scrolling Background Objects 0 Answers

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

What's wrong with my script? 1 Answer

extra GameObjects Spawning 2 Answers

Spawning prefabs dependant upon Health UI? 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