Search Unity

Objects not spawning?

Discussion in 'Android' started by Corva-Nocta, Dec 14, 2018.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    My game has an odd little problem where things work perfectly on my pc but don't run very well on my phone.

    When I play the game and within the game select which level I want to play, it will switch scenes, all good so far on both platforms. But when a new level is loaded my player is instantiated on PC but on mobile won't instantiate. I'm not sure what the problem is exactly but I think it is a timing issue.

    I have a GameController object in my main scene (where I select levels and stuff) which has a DontDestroyOnLoad script attached to it. Within each level is an object with a LevelController, the LevelController will grab the GameController and tell it to spawn the player.

    The problem seems to be here, where it won't grab the GameController for some reason (on mobile, on pc its fine) I have tried setting the code to both:
    Code (csharp):
    1. void Start()
    2. {
    3.    gameController.Spawn();
    4. }
    and I have tried:
    Code (csharp):
    1. void Awake()
    2. {
    3.    gameController.Spawn();
    4. }
    Neither one works. Is there another way I can make the level grab the GameController?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Have you check whether that script actually runs on phone?
    Try putting Debug.Log() on both sides of it and check the logcat.
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Will Debug.Log show up on mobile? I don't know of any way to check the console while playing on my phone (and the Unity5 remote app doesn't work for some reason) so the only way I have to check things is by obscure means. But if Debug.Log will actually show a message on the screen then that could help a ton for me
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You have to use adb tool from Android SDK. "adb logcat" command will give you athe output.
    Or you can install our logcat console package and see output in Unity Editor.
    Alternatively you can export Android Studio project from Unity and use Android Studio, which can also show you log output.
     
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Does that run on the pc or on the phone itself? Can I see that information on my phone or only when I hook it up to my pc?

    For some reason Unity can't find my phone, unless I build my game then it finds it just fine
     
  6. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I've narrowed down my problem to one specific area, and I have no idea why it is doing this. On mobile, when I switch from the main menu scene to a specific level scene, my LevelController scrilt does not run Start() function, or Awake() function if you switch it to that.

    Why would it not run either of those functions when the scene is loaded?

    Again, it only does this in mobile.
     
  7. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Ok, I finally got it to run and spawn my objects, except now it is spawning the objects in a random location and I have no idea why. It is spawning my objects at (25, 0, 0) which makes no sense. I don't have any objects at those coordinates, and I don't have anything that sets them to those coordinates. Why is it choosing this super random location to instantiate?

    I have my code set to instantiate then set the position to where I want, but it clearly is not grabbing that second part.

    Here is the code:
    Code (csharp):
    1. public void SpawnVehicle()
    2. {
    3.    GameObject vehicleSpawn = GameObject.FindGameObjectWithTag("StartNode");
    4.    GameObject playerVehicle = Instantiate(vehicle) as GameObject;
    5.    playerVehicle.transform.position = vehicleSpawn.transform.position;
    6.  
    7.    SpawnPlayer();
    8.    SpawnRunner();
    9. }
    Everything seems to run fine, except for setting the playerVehicle to the position it is supposed to be in. I guess it could be that the level isn't finding the StartNode object?
     
  8. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    So I tried setting the whole function on a 2 second countdown timer, just to see if it was a script timing issue where it was trying to find the gameobject before it was actually loaded. Definitely not the issue.

    For some reason it won't find the gameobject with the tag that I want. I've gone over every inch of code and can't find why this would be the case. I have other scripts that are finding game objects with tags, but for some reason this one line isn't working.

    What is extra weird is that it runs perfectly on the pc, not a single issue anywhere to be found. But when I move to mobile this one specific line suddenly doesn't run. Does anyone have any advice at all? This makes no sense at all
     
  9. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    So, I got it working. All I did was close Unity and restart. Now it all works fine. Weird that that would be the issue, but it seems to be working perfectly now.

    Guess the ol' turn it off and back on again was the answer :p