Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HameedAbdul · Jul 12, 2016 at 08:02 AM · c#unity 52dcollisionprefab

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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! :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Button Enabling/Disabling using Collision Triggers? 1 Answer

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

1 Button Alternating Between 2 onClick Functions 1 Answer

How do you flip the bullet trail prefab, so it shoots the way you are facing and how do you make the bullets disappear when they hit a platform? 1 Answer

My rigidbody2d is passing through the side of a collider2d but it's working on the top 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges