Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 15, 2018 at 06:44 AM by Rachens for the following reason:

Solved

avatar image
0
Question by Rachens · Mar 14, 2018 at 09:09 PM · unity 5unity 2dplatformertutorial

"buddies" multiplying, can't find what's wrong with my script

so I'm following this old tutorial: https://www.youtube.com/watch?v=77zdOaUGguc but my background just keeps multiplying all over the place, crashing Unity if I don't stop the game fast enough. I've looked through the comments and made the changes that people there suggested but it's an old video so i doubt I'll get any answers if i ask there.

My script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(SpriteRenderer))]
 
 public class Tiling : MonoBehaviour
 {
 
     public int offsetX = 2;
 
     public bool hasARightBuddy = false;
     public bool hasALeftBuddy = false;
 
     public bool reverseScale = false;
 
     private float spriteWidth = 0f;
     private Camera cam;
     private Transform myTransform;
 
     void Awake()
     {
 
         cam = Camera.main;
         myTransform = transform;
 
     }
     // Use this for initialization
     void Start()
     {
 
         SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
         spriteWidth = sRenderer.sprite.bounds.size.x;
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (hasALeftBuddy == false || hasARightBuddy == false)
         {
 
             float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
 
             float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
             float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;
 
             if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
             {
 
                 MakeNewBuddy(1);
                 hasARightBuddy = true;
 
             }
             else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false) ;
         }
 
         MakeNewBuddy(-1);
         hasALeftBuddy = true;
 
     }
 
     void MakeNewBuddy(int rightOrLeft)
     {
         Vector3 newPosition = new Vector3(myTransform.position.x + myTransform.localScale.x * spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
         Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;
 
         if (reverseScale == true)
         {
 
             newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z);
 
         }
 
         newBuddy.parent = myTransform;
         if (rightOrLeft > 0)
         {
 
             newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
 
         }
         else
         {
 
             newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
 
         }
     }
 
 }
 
 
 
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

  • Sort: 
avatar image
0
Best Answer

Answer by Rachens · Mar 15, 2018 at 06:44 AM

OK so I solved it. I didn't ever open the "else if", all i had to was:

             else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
             {
 
                 MakeNewBuddy(-1);
                 hasALeftBuddy = true;
 
             }
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

Follow this Question

Answers Answers and Comments

158 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

Related Questions

Modification to Unity2D Roguelike 1 Answer

DllNotFoundException: Anima2D 0 Answers

Getting trouble in migrating the Unity IAP old version to new version of IAP in existing project 0 Answers

How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer

How to stick to an object when a button is pressed Unity 2d 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