Sprites are invisible when passed between scenes
I am making discs that are circular double sided so you can read it both ways, the pictures will be taken from the gallery from the menu scene into the main scene. Currently when I try to pass an image into the next scene it's invisible.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Niantic.ARDK.AR;
using Niantic.ARDK.Utilities;
using Niantic.ARDK.AR.ARSessionEventArgs;
using Niantic.ARDK.AR.HitTest;
using UnityEngine.UI;
using TMPro;
public class Main : MonoBehaviour
{
public GameObject character;
IARSession session;
public Camera camera;
public TMP_InputField inputField;
public TextMesh tm;
void Start()
{
ARSessionFactory.SessionInitialized += OnSessionInitialized;
}
private void OnSessionInitialized(AnyARSessionInitializedArgs args)
{
ARSessionFactory.SessionInitialized -= OnSessionInitialized;
session = args.Session;
}
void Update()
{
if(PlatformAgnosticInput.touchCount <= 0) { return; }
var touch = PlatformAgnosticInput.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
TouchBegan(touch);
}
}
private void TouchBegan(Touch touch)
{
var currentFrame = session.CurrentFrame;
if(currentFrame == null)
{
return;
}
if(camera == null)
{
return;
}
var hitTestResults =
currentFrame.HitTest
(
camera.pixelWidth,
camera.pixelHeight,
touch.position,
ARHitTestResultType.ExistingPlaneUsingExtent |
ARHitTestResultType.EstimatedHorizontalPlane
);
if(hitTestResults.Count == 0)
{
return;
}
//spawn ball into world
GameObject ch = Instantiate(character, new Vector3(0, 0, 0), Quaternion.identity);
//the 1st child is outer sphere
GameObject ChildGameObject1 = ch.transform.GetChild (1).gameObject;//the 2nd is the 1st disc
GameObject ChildGameObject2 = ch.transform.GetChild (2).gameObject;//the 3rd is the 2nd disc
ch.transform.position = hitTestResults[0].WorldTransform.ToPosition();//starts by setting the postion
ch.transform.position = new Vector3(ch.transform.position.x, ch.transform.position.y + .9f, ch.transform.position.z);
ch.GetComponent<Rigidbody>().velocity = Random.onUnitSphere * .5f; //shoots ball at random location
//sets the discs to a custom sprite
ChildGameObject1.GetComponent<SpriteRenderer>().sprite = GetComponent<ChangeSprite>().getSprite();
ChildGameObject2.GetComponent<SpriteRenderer>().sprite = GetComponent<ChangeSprite>().getSprite();
//takes string from the main menu
ch.GetComponent<TextMesh>().text = PlayerPrefs.GetString("input");
}
}
this is the sprite picker class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Picker : MonoBehaviour
{
public GameObject obj;
public GameObject change;
void Start()
{
}
// Update is called once per frame
void Update()
{
// if( NativeGallery.IsMediaPickerBusy() )
// return;
// PickImage( 512 );
}
public void PickImage( int maxSize )
{
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery( ( path ) =>
{
Debug.Log( "Image path: " + path );
if( path != null )
{
// Create Texture from selected image
Texture2D texture = NativeGallery.LoadImageAtPath( path, maxSize );
if( texture == null )
{
Debug.Log( "Couldn't load texture from " + path );
return;
}
//obj.GetComponent<ImageGen>().Init(this.gameObject);
GetComponent<ChangeSprite>().Init(GetComponent<SpriteRenderer>(), Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)));
obj.GetComponent<SpriteRenderer>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
} );
Debug.Log( "Permission result: " + permission );
}
}
this is the change sprite class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
private static ChangeSprite instance;
private SpriteRenderer rend;
private Sprite sprite;
void Start()
{
if(instance != null)
{
Destroy(this.gameObject);
return;
}
instance = this;
//stops object form deleting when changing scenes
GameObject.DontDestroyOnLoad(this.gameObject);
rend = GetComponent<SpriteRenderer>();
rend.sprite = sprite;
}
public void Init(SpriteRenderer rend, Sprite sprite)
{
this.rend = rend;
this.sprite = sprite;
}
public Sprite getSprite()
{
return sprite;
}
}
I think these are all of the relevant classes.
Comment