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 SpaceFace58 · Mar 08, 2020 at 11:38 PM · backgroundscrollparallax

Why does only one of my instantiated scripts work?

(TL:DR "My prefab is supposed to create another one of itself if you keep going in a direction, but it only does this in one direction(the one you go to first), the other direction doesn't work)

So, I have my first game where a plane flies around

I want some clouds to appear behind the plane but they only generate in front of the plane and get deleted behind it.

I have done this by having a background prefab, a left border, and an instantiate position. The prefab contains the background, the left border, and the instantiate position.

Once the player reaches the left border's x position, the prefab instantiates at the instantiate position. This works pretty well, however, when I tried to copy and paste the same script (with some changes for the right side), and another border and instantiate position for the right side, it doesn't work.

It lets me go one direction and it looks like it's supposed to, but when I go the opposite direction, it doesn't, it's weird.

Here is the code for the left side:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LoopLeftTest : MonoBehaviour
 {
     private GameObject empty;
     private GameObject Player;
     private GameObject right;
     private bool dio = true;
     private GameObject bor;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Player = GameObject.Find("Player");
         empty = GameObject.Find("Empty");
         right = GameObject.Find("Lefto");
         bor = gameObject;
 
         if (Player.transform.position.x < bor.transform.position.x + 2)
         {
             if (Player.transform.position.x > bor.transform.position.x - 2)
             {
                 if (dio == true)
                 {
                     Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
                     dio = false;
                 }
             }
         }
     }
 }
 

and here's the code for the right side:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LoopRightTest : MonoBehaviour
 {
     private GameObject empty;
     private GameObject Player;
     private GameObject right;
     private bool dio = true;
     private GameObject bor;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Player = GameObject.Find("Player");
         empty = GameObject.Find("Empty");
         right = GameObject.Find("Righto");
         bor = gameObject;
 
         if (Player.transform.position.x < bor.transform.position.x + 2)
         {
             if (Player.transform.position.x > bor.transform.position.x - 2)
             {
                 if (dio == true)
                 {
                     Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
                     dio = false;
                 }
             }
         }
     }
 }
 

When I go one direction, it's supposed to loop, and it does, (one of these scripts keeps activating), but when I go the opposite direction, it doesn't loop!

Thank you very much -Spaceface

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

2 Replies

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

Answer by streeetwalker · Mar 09, 2020 at 03:58 AM

it's hard to understand from your description what you are trying to do. It sounds like you want something to wrap from the right edge to the left edge if it moves off the right edge, and the opposite action if it moves off the left. (it is not clear if you are talking about the airplane wrapping around or the clouds!)

So, simplify it. You should only use 1 script - you do not need 2 scripts!!!

As is, the code doesn't distinguish between hitting the left or right. Here's the algorithm you should use:

  If it is beyond the left bounds, do your right thing 
  ELSE if it is beyond the right bounds, do your left thing 


you also talk about the problem of clouds instantiating in front of the airplane? If you have a background gameobject that is behind the plane, simply parent the cloud game object to that object immediately after you instantiate it. for example:

 GameObject cloud = (GameObject) intantiate( ...... );
 cloud.parent = backGroundGameObject;

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 SpaceFace58 · Apr 16, 2020 at 09:57 PM 0
Share

Thank you, I was very new to unity when I posted this and this helped me, along with more experience, thank you.

avatar image
0

Answer by Reid_Taylor · Apr 17, 2020 at 02:54 AM

Also you should not be using GameObject.Find("") to get references. That method is semi heavy as it is by putting it in update it just increases its size. This should happen in start or just slide in references in inspector. And resource.load is heavy. You should have reference of prefab, then just instantiate reference.

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

202 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 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 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 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

Scrolling my background Unity 5 0 Answers

How to loop object smoothly through positions in array/Changing scrolling background 0 Answers

Parallax Scrolling Performance Issue - Gets Laggy after a bit 0 Answers

How to make a 2D autoscrolling level 0 Answers

2D scrolling background 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