I'm testing my mobile game for iPhone users where you have to throw a fork to another game object. Now these fork game objects are on a List which I'm spawning a total of 8 forks on the scene but you throw one at a time. When I run the game on the Unity Editor, the game works perfectly fine. Now, when I built my game and run it on my iPhone for the very first time, the first fork that I throw to the pizza game Object do not attach to it and I have to entirely close the mobile game on my phone and re-open the game. Once I do re-open the game the first fork does attach to it, but not the second fork. The cycle repeats itself, I close the mobile game again on my phone, re-open it and the first and second fork does attach to the game object, but not the third fork.. the pattern and cycles just go on...Does anyone have any idea what could've caused this issue or how to fix it? Basically I'm closing the game every single time.. Have you guys seen this before? Please any assistance/feedback would be so highly appreciate it!!!!
Game Controller Script
public void ForkSpawn()
{
forkCount--;
if (forkObject != null)
{
Debug.Log("ForObject is not null. Testing Instantiate");
//TESTING; Assigning Instantiate GameObject;
GameObject spawnedFork = Instantiate(forkObject, forkSpawnPosition, Quaternion.identity);
//TESTING*Can be remove.. Resetting values;
ResettingforkValues(spawnedFork);
//It will force physics update before fork becomes active;
//This ensure that Unity immediately updates physics interactions;
Physics2D.SyncTransforms();
//TESTING; Track the Spawn Objects on the List;
spawnedForks.Add(spawnedFork);
//Decrement just after successful spawn of the GameObject; TESTING;
//forkCount--;
}
else
{
Debug.Log("Fork Object is not assigned or either assigned properly");
}
}
private void ResettingforkValues(GameObject fork)
{
Rigidbody2D rb = fork.GetComponent<Rigidbody2D>();
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
rb.bodyType = RigidbodyType2D.Dynamic;
rb.velocity = Vector2.zero;
rb.gravityScale = 0;
rb.constraints = RigidbodyConstraints2D.None;
fork.transform.SetParent(null);
//ForkScript.Instance.isActive = true;
ForkScript forkScript = fork.GetComponent<ForkScript>();
if (forkScript != null)
{
//forkScript.isActive = true;
StartCoroutine(forkScript.EnableCollision());
}
}
ForkScript class for the fork gameObject
private void FixedUpdate()
{
if (startThrowingFork)
{
rb.AddForce(throwForce, ForceMode2D.Impulse);
rb.gravityScale = 1;
//Calling the Decrementing method for the UI Fork image;
GameController.Instance.GameUI.DecrementDiplayForkCount();
startThrowingFork = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//..if is not active; We don't want to detect collisions if the Fork object is not active
if (!isActive)
return;
//Setting value back to False;
isActive = false;
//TESTING: This.transform.position.y;
if (collision.collider.tag == "PizzaLog")
{
//Adding special sound where the fork hits..
AudioManager.Instance.Play("ForkHitSound");
//Once we have noticed the collision, play particles effect;
GetComponent<ParticleSystem>().Play();
//Displaying current Score;
GameM.Instance.savingScore();
//..Stopping the movement of the Fork;
rb.velocity = new Vector2(0, 0);
//TESTING
rb.gravityScale = 0;
//Reseting angular velocilty as well;
//rb.angularVelocity = 0f;
rb.bodyType = RigidbodyType2D.Kinematic;
//Freezing all contraints:
rb.constraints = RigidbodyConstraints2D.FreezePosition;
// ..To be equal to the parent GameObject of the Fork Game Object;
//this.transform.SetParent(collision.collider.transform);
StartCoroutine(AttachForkToPizza(collision.collider.transform));
//TESTING;
StartCoroutine(SafetyCheck());
//Setting new offset for x values for the forkCollider
//We're not messing the y coordinates;
forkCollider.offset = new Vector2(forkCollider.offset.x, -0.4f);
forkCollider.size = new Vector2(forkCollider.size.x, 1.2f);
//..It was a success hit, method;
GameController.Instance.OnSuccessfulForkHit();
}
else if (collision.collider.tag == "ForkObject")
{
//We're not touching the Y axis;
rb.velocity = new Vector2(rb.velocity.x, -2);
//TESTING
rb.gravityScale = 1;
//..Start the GameOverSequence down here;
GameController.Instance.StartGameOverSequence(false);
}
}
public IEnumerator AttachForkToPizza(Transform pizza)
{
yield return new WaitForFixedUpdate(); // Wait for the next physics to update;
transform.SetParent(pizza);
Debug.Log("Fork Successfully attached to the Pizza GameObject");
}
public IEnumerator EnableCollision()
{
yield return new WaitForFixedUpdate();
isActive = true;
}
private IEnumerator SafetyCheck()
{
yield return new WaitForSeconds(0.2f);
if (rb.bodyType == RigidbodyType2D.Kinematic && Mathf.Abs(rb.velocity.magnitude) < 0.01f)
{
forkFrozenSafetyCheck = true;
Debug.Log("Fork did set in place properly");
}
else
{
//Handling Failure;
GameController.Instance.OnSuccessfulForkHit();
}
}
I'm testing my mobile game for iPhone users where you have to throw a fork to another game object. Now these fork game objects are on a List which I'm spawning a total of 8 forks on the scene but you throw one at a time. When I run the game on the Unity Editor, the game works perfectly fine. Now, when I built my game and run it on my iPhone for the very first time, the first fork that I throw to the pizza game Object do not attach to it and I have to entirely close the mobile game on my phone and re-open the game. Once I do re-open the game the first fork does attach to it, but not the second fork. The cycle repeats itself, I close the mobile game again on my phone, re-open it and the first and second fork does attach to the game object, but not the third fork.. the pattern and cycles just go on...Does anyone have any idea what could've caused this issue or how to fix it? Basically I'm closing the game every single time.. Have you guys seen this before? Please any assistance/feedback would be so highly appreciate it!!!!
Game Controller Script
public void ForkSpawn()
{
forkCount--;
if (forkObject != null)
{
Debug.Log("ForObject is not null. Testing Instantiate");
//TESTING; Assigning Instantiate GameObject;
GameObject spawnedFork = Instantiate(forkObject, forkSpawnPosition, Quaternion.identity);
//TESTING*Can be remove.. Resetting values;
ResettingforkValues(spawnedFork);
//It will force physics update before fork becomes active;
//This ensure that Unity immediately updates physics interactions;
Physics2D.SyncTransforms();
//TESTING; Track the Spawn Objects on the List;
spawnedForks.Add(spawnedFork);
//Decrement just after successful spawn of the GameObject; TESTING;
//forkCount--;
}
else
{
Debug.Log("Fork Object is not assigned or either assigned properly");
}
}
private void ResettingforkValues(GameObject fork)
{
Rigidbody2D rb = fork.GetComponent<Rigidbody2D>();
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
rb.bodyType = RigidbodyType2D.Dynamic;
rb.velocity = Vector2.zero;
rb.gravityScale = 0;
rb.constraints = RigidbodyConstraints2D.None;
fork.transform.SetParent(null);
//ForkScript.Instance.isActive = true;
ForkScript forkScript = fork.GetComponent<ForkScript>();
if (forkScript != null)
{
//forkScript.isActive = true;
StartCoroutine(forkScript.EnableCollision());
}
}
ForkScript class for the fork gameObject
private void FixedUpdate()
{
if (startThrowingFork)
{
rb.AddForce(throwForce, ForceMode2D.Impulse);
rb.gravityScale = 1;
//Calling the Decrementing method for the UI Fork image;
GameController.Instance.GameUI.DecrementDiplayForkCount();
startThrowingFork = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//..if is not active; We don't want to detect collisions if the Fork object is not active
if (!isActive)
return;
//Setting value back to False;
isActive = false;
//TESTING: This.transform.position.y;
if (collision.collider.tag == "PizzaLog")
{
//Adding special sound where the fork hits..
AudioManager.Instance.Play("ForkHitSound");
//Once we have noticed the collision, play particles effect;
GetComponent<ParticleSystem>().Play();
//Displaying current Score;
GameM.Instance.savingScore();
//..Stopping the movement of the Fork;
rb.velocity = new Vector2(0, 0);
//TESTING
rb.gravityScale = 0;
//Reseting angular velocilty as well;
//rb.angularVelocity = 0f;
rb.bodyType = RigidbodyType2D.Kinematic;
//Freezing all contraints:
rb.constraints = RigidbodyConstraints2D.FreezePosition;
// ..To be equal to the parent GameObject of the Fork Game Object;
//this.transform.SetParent(collision.collider.transform);
StartCoroutine(AttachForkToPizza(collision.collider.transform));
//TESTING;
StartCoroutine(SafetyCheck());
//Setting new offset for x values for the forkCollider
//We're not messing the y coordinates;
forkCollider.offset = new Vector2(forkCollider.offset.x, -0.4f);
forkCollider.size = new Vector2(forkCollider.size.x, 1.2f);
//..It was a success hit, method;
GameController.Instance.OnSuccessfulForkHit();
}
else if (collision.collider.tag == "ForkObject")
{
//We're not touching the Y axis;
rb.velocity = new Vector2(rb.velocity.x, -2);
//TESTING
rb.gravityScale = 1;
//..Start the GameOverSequence down here;
GameController.Instance.StartGameOverSequence(false);
}
}
public IEnumerator AttachForkToPizza(Transform pizza)
{
yield return new WaitForFixedUpdate(); // Wait for the next physics to update;
transform.SetParent(pizza);
Debug.Log("Fork Successfully attached to the Pizza GameObject");
}
public IEnumerator EnableCollision()
{
yield return new WaitForFixedUpdate();
isActive = true;
}
private IEnumerator SafetyCheck()
{
yield return new WaitForSeconds(0.2f);
if (rb.bodyType == RigidbodyType2D.Kinematic && Mathf.Abs(rb.velocity.magnitude) < 0.01f)
{
forkFrozenSafetyCheck = true;
Debug.Log("Fork did set in place properly");
}
else
{
//Handling Failure;
GameController.Instance.OnSuccessfulForkHit();
}
}
Share
Improve this question
asked Feb 2 at 23:49
Amaury C.Amaury C.
511 silver badge7 bronze badges
1
- Issue has been resolved now. Deleted all data related to the PlayerPrefs from my computer and I was able to replicate the same issue on the UnityEditor. The issue was related to some scripts not being attached to another game object which for whatever reason was crashing the instantiate of the other GameObject in question from the list! Again, issue resolved – Amaury C. Commented Feb 4 at 0:30
1 Answer
Reset to default 0I would use a runtime debugger, to debug the current state of each of the forks during runtime, https://assetstore.unity/packages/tools/gui/in-game-debug-console-68068 is a free one I use.
iOS is weird with certain runtime physics checks, I've encountered issues myself where the OnTriggerEnter2D would not execute for whatever reason.
I would add a OnCollisionStay2D event, maybe its not catching the initial enter state, but it should if the object stays inside the trigger.
I would log the current state of 'isActive' and the rigidbodies kinematic state, as well as any collisions/gameobjects that enter or exit
Log each section of the gameplay loop involving forks, find where its not behaving like intended, and that should be your issue.
The only thing that stands out code wise, is
//..if is not active; We don't want to detect collisions if the Fork object is not active
if (!isActive)
return;
Make sure isActive is not being set to false at the wrong time, for whatever reason whenever you restart the game it looks like isActive gets set to false incrementally. I would investigate this during runtime. This will cause it to not do anymore checks.
I would probably just remove the return; and see what happens.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745253647a4618827.html
评论列表(0条)