Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Aug 03, 2014 at 10:24 PM · c#gameobjectplayer

C# Script is affecting multiple GameObjects but I would like it to only affect one of them...

As a part of my game, there is a mechanic for falling platforms. When I first added a falling platform, I thought it would work, but I placed another one later on in the level and it turned out that when I triggered one of the falling platforms, it affected the other one.

Here are my scripts:

Trigger Script -

 using UnityEngine;
 using System.Collections;
 
 public class FallingScriptTrigger : MonoBehaviour {
 
     void OnTriggerEnter (Collider other)
     {
         if(other.tag == "Player")
         {
             fallingScript.PlayerIsOnTop = true;
         }
     }
 
     void OnTriggerExit (Collider other)
     {
         if(other.tag == "Player")
         {
             fallingScript.PlayerIsOnTop = false;
         }
     }
 }
 

Controller Script -

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [AddComponentMenu("Platform/Falling Platform Script")]
 public class fallingScript : MonoBehaviour {
 
     //Sound Prefabs
     public GameObject IndicatorSound;
     public GameObject fallingSound;
 
     //Indication GameObjects
     public GameObject indicator1;
     public GameObject indicator2;
     public GameObject indicator3;
 
     //Variables
     public float intervalTime;
     public static bool PlayerIsOnTop = false;
     bool OneTime = true;
     Vector3 startPos;
     bool hasStartedFalling = false;
 
     // Use this for initialization
     void Start () {
         IndSetAct(false, false, false);
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         startPos = transform.position;
     }
 
     void Update ()
     {
         if(PlayerIsOnTop == true)
         {
             if(OneTime == true)
             {
                 StartCoroutine("seq");
                 OneTime = false;
             }
         }
         else
         {
             if(hasStartedFalling == false)
             {
                 if(transform.position == startPos)
                     {
                         OneTime = true;
                         StartCoroutine("sseq");
                     }
             }
         }
     }
 
     void IndSetAct (bool setActiveBool1, bool setActiveBool2, bool setActiveBool3)
     {
         indicator1.SetActive(setActiveBool1);
         indicator2.SetActive(setActiveBool2);
         indicator3.SetActive(setActiveBool3);
     }
 
     IEnumerator seq ()
     {
         IndSetAct(true, false, false);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(true, true, false);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(true, true, true);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(false, false, false);
         Instantiate(fallingSound);
         rigidbody.constraints = RigidbodyConstraints.None;
         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
         hasStartedFalling = true;
         yield return null;
     }
     IEnumerator sseq ()
     {
         StopCoroutine("sseq");
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         IndSetAct(false, false, false);
         transform.position = startPos;
         yield return null;
     }
 }

Please help asap... I can't really progress the game's development without fixing this bug. Thanks...

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
1
Best Answer

Answer by flaviusxvii · Aug 04, 2014 at 03:05 AM

 public static bool PlayerIsOnTop = false;

You should look up what static means. It's the problem.

Comment
Add comment · Show 1 · 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
avatar image $$anonymous$$ · Aug 06, 2014 at 05:55 PM 0
Share

Its so simple. Im sorry, but when I uploaded this post, it was 1am and I was tired. Its so stupid how I didnt understand this... Oh well. What I did was I combined the two scripts so that the OnTriggerEnter/Exit was all controlled from the same place. I know I could have done it a more efficient way but, hey, what I did works. Thanks to flaviusxvii for re$$anonymous$$ding me. (I facepalmed so hard on this one.)

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

2 People are following this question.

avatar image avatar image

Related Questions

Using script's method from all of the gameobjects that has that script 2 Answers

c# destroy gameobject on 0 hp 2 Answers

Player 1 and Player 2 move together 1 Answer

Having trouble swapping GameObjects on button click 1 Answer

Problem to play animation in C# 5 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