- Home /
How to access a prefab's boolean property through collision (with the same Prefab but different Instance)?
I am a trying to recreate the first stage of Super Mario Brothers, but I am having a bit of trouble with the collision system between Koopa Shells. I have written two different scripts in hopes to achieve this:
This First script is to handle the Basic movement and start shell movement on Player Collision
It's Rather long so I took the liberty of including only parts that were in direct correlation/ causation of the problem
public class KoopaShell : MonoBehaviour
{
//Makes this accessible to other Scripts
public bool Moving;
// Boolean Flags to be inherited
bool shellLeft, shellRight, rightBlocked, leftBlocked;
public float rayDis;
public LayerMask enemyMask;
public Transform rCast;
[... Rest of the code..]
void OnCollisionEnter2D(Collision2D coll)
{
if (leftBlocked || rightBlocked) // If the either side of the shell is hit
if (coll.gameObject.tag == "Player" && Moving == false) // If the player makes contact with a resting shell
{
MoveShell();
}
}
public IEnumerator MoveShell() //Courotine for 2D Collision
{
//The Courotine will wait for the end of the frame
yield return new WaitForFixedUpdate();
//Then set moving to true
Moving = true;
}
}
The Second Script handles all other collision and inherits from the first in an attempt to simplify the calling from script to script.... Could also be the cause of my problem.
using UnityEngine;
using System.Collections;
public class MovingShellScript : KoopaShell
{
//This is a continuation of the KoopaShell Script
//Dealing with 2D Collision
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Koopa Shell") // if contact is made with another shell that is moving
if(col.gameObject.Equals(Moving) == false) //if the shell is not moving
{
{
Destroy(col.gameObject); // Then the resting shell is destroyed
Debug.Log("Looks like another shell is unemployed");
}
}
if (col.gameObject.tag == "Enemy") // If a Enemy is hit by the Shell
if (Moving == true) // and If the Shell is moving
{
// The GameObject hit is Destroyed
Destroy(col.gameObject);
}
}
}
Currently the script above does not destroy the various gameobjects under the Enemy and Player tag. Maybe this could be done implicitly with an associated script to the desired gameobject that correlates with the tag....**
Also the script destroys both shells during a collision. I don't understand why my various boolean flags aren't creating the desired result(s):**
If both shells are moving then both shells are destroyed.
If only one shell is moving then the resting shell is destroyed.
I am seeking to understand the error in my logic for this to work as desired.
Thank you for your time!
Answer by HameedAbdul · Jul 16, 2016 at 04:48 PM
Firstly, my Moving Boolean was a static variable(this was changed before I asked this question). Secondly, I failed to reference the object I was colliding with thus building on my first logic error of the static Boolean - which made all instances share the same value. Lastly, after realizing the later of the two and the fact that my code was not properly allowing the entire engine to access the value of the Moving Boolean - which was kindly pointed out to me to check, it was then when I realized I was prematurely calling my coruotine without the use of StartCoruotine().
IEnumerator MoveShell() //Courotine for 2D Collision
{
//The Courotine will wait for the end of the frame
yield return new WaitForSeconds(0.001f);
//Then set moving to true
yield return Moving = true;
}
void OnCollisionEnter2D(Collision2D coll)
{
// Any collision that deals with the Player will be here
if (leftBlocked || rightBlocked) // If the either side of the shell is hit
if (coll.gameObject.tag == "Player" && this.Moving == false) // If the player makes contact with a resting shell
{
StartCoroutine(MoveShell()); // Calls our Courotine
//Moving = true; //Makes sure that are boolean flag is set to true after the courotine starts
}
// Any collision that deals with other Koopa Shells will be here
if (coll.gameObject.CompareTag("Koopa Shell")) // if contact is made with another shell
{
// Get a reference to the other shell:
KoopaShell otherShell = coll.gameObject.GetComponent<KoopaShell>();
if (otherShell.Moving == false && this.Moving == true) //if one of the shells is at rest
{
Destroy(coll.gameObject); // Then the resting shell is destroyed
Debug.Log("The Shell at rest was destroyed");
}
if(otherShell.Moving == true && this.Moving == true)// if both shells are not moving
{
Destroy(this.gameObject); //Destroy the moving shell that this script is attached to
Destroy(coll.gameObject); //Destroy the moving shell that is being collided with
Debug.Log("Both Shells involved were destroyed");
}
}
}
The Actually code is no where as near as this broken up. Please forgive the failed attempt of presentation.
Thank you again for all that followed or assisted me in any way! :)