Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Prefab spawn script not Working on android.

Discussion in 'Scripting' started by alexandrumarcel, Jan 12, 2020.

  1. alexandrumarcel

    alexandrumarcel

    Joined:
    Dec 15, 2019
    Posts:
    9
    Hi! I'm trying to remake the Flappy Bird game but I have one small problem: The pipes won't spawn. The thing is that in unity works fine but on android nothing is spawned.

    Here is my code for the spawning mechanism:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PipeSpawner : MonoBehaviour
    6. {
    7.  
    8.     public static PipeSpawner Instance;
    9.     public float maxTime = 0.5f;
    10.     private float timer = 0;
    11.     public float height = 1.5f;
    12.  
    13.     public List<Transform> pipeList;
    14.     public GameObject pipe;
    15.     // Start is called before the first frame update
    16.     void Awake()
    17.     {
    18.         pipeList = new List<Transform>();
    19.  
    20.         if (Instance == null)
    21.         {
    22.             Instance = this;
    23.         }
    24.     }
    25.  
    26.     void Start()
    27.     {
    28.         GameObject newPipe = Instantiate(pipe);
    29.         newPipe.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 0);
    30.         pipeList.Add(newPipe.transform);
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (timer > maxTime)
    37.         {
    38.             GameObject newPipe = Instantiate(pipe);
    39.             //pipeObject.position += new Vector3(-1, 0, 0) * GameControl.Instance.scrollSpeed * Time.deltaTime;
    40.             newPipe.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 0);
    41.  
    42.             timer = 0;
    43.             pipeList.Add(newPipe.transform);
    44.         }
    45.         timer += Time.deltaTime;
    46.  
    47.     }
    48. }
    49.  
    I also have a small script for the pipes to move them and destroy them:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PipeControl : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         transform.position += new Vector3(-1, 0, 0) * GameControl.Instance.scrollSpeed * Time.deltaTime;
    17.         if (transform.position.x < -14.96f)
    18.         {
    19.             Destroy(transform.gameObject);
    20.         }
    21.     }
    22. }
    23.  
     
  2. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Actually i don't know, but i may give you some hints:
    1- Are you sure your script is executed on the device (if not sure try to add a sprite and change it's color in the Start function) to make sure that your script is executed.
    2- Do you use any material for the pipe sprite (if yes, maybe the material do not work on android).
    3- Try to instantiate them at position (0,0,0) to make sure that it is not a canvas problem or a camera position or a screen ratio.
    4- If nothing works, then may god be with you :).
     
  3. alexandrumarcel

    alexandrumarcel

    Joined:
    Dec 15, 2019
    Posts:
    9
    It looked that the object spawned but the problem is the resolution scale. Everything on my phone looks bigger than in Unity. I'm currently looking for a fix to that.
     

    Attached Files:

  4. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    do you use a canvas to scale the pipes or instantiating the pipes outside the canvas?
    if using a canvas, then correct it's settings or watch a tutorial for canvases on YouTube.