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 /
  • Help Room /
avatar image
0
Question by Besi2019 · Feb 23, 2018 at 11:40 PM · scripting beginnercheckpoint

Deactivate 1st checkpoint, after 2nd checkpoint is activated

Hi there

I've created 2 checkpoints in my level, and now I want to add a script, which tells the 1st checkpoint, that when the player hits the 2nd checkpoint, the 1st checkpoint should be deactivated. Which means, when the player goes back, and hits the 1st checkpoint again, after hitting the 2nd checkpoint, the player should still go back to the 2nd checkpoint.

(The flag on 1st checkpoint should still be up, even though it is deactivated)

Hope that makes since.

This is my code, so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckpointController : MonoBehaviour {
     
     public Sprite flagClosed;
     public Sprite flagOpen;
     
     private SpriteRenderer theSpriteRenderer;
     
     public bool checkpointActive;
 
     // Use this for initialization
     void Start () {
         theSpriteRenderer = GetComponent<SpriteRenderer>();
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "Player")
         {
             theSpriteRenderer.sprite = flagOpen;
             checkpointActive = true;
         }
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dgoyette · Feb 24, 2018 at 01:27 AM

I index my checkpoints with an integer, and I keep track of the max checkpoint index the player has trigger. If a player goes back and enters an earlier checkpoint area, I only trigger the checkpoint logic if the checkpoint's index is less than the maximum checkpoint index the player has triggered.

Another approach would be just to disable the earlier checkpoint script/gameobject upon triggering the second checkpoint.

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 Besi2019 · Feb 24, 2018 at 10:02 AM 0
Share

Do you do that in the script? I am a beginner, and just learning.

avatar image
0

Answer by UrielKane · Feb 25, 2018 at 09:31 AM

This seems a case where "emumerators" are usefull. Im not a profesional programer so if speak wrong let me know. But as far as i know, enumerators can store diferent "states" wich is better than using like boolean variables wich store only 2 values. The enumerator wich appear like "enum" in script. Can store only one active value from the ones you setup to be. For example

 public enum DoorState { Closed = 0, Opening = 1, Opened = 2, Closing = 3, Stoped = 4}
 public DoorState DoorBehave;

I use the enum for my door script state machine wich can be only in one state at the time and store several state that can be. In my case i added a five state wich is stopped, once that state is reach the door will never be opened or closed again. The code you should have in the case you use enums should be like.

 public enum checkpointStates { inactive = 0, Active = 1, finished = 2}
 public checkpointStates checkPStatus;
 
 public void EnteringCheckPoint () {
 switch(checkPStatus){
 case checkpointStates.inactive :
 //check if the checkpoint is reached and if so change the state machine.
 print("im inactive");
 break;
 case checkpointStates.Active :
 // if its active maybe you want this to call some events put them here.
 print("im active");
 break;
 case checkpointStates.finished :
 //Make your flag to appear like active or whatever and then just return.
 print("i finished my job");
 return;
 break;
 }
 }

I have to say that this code is not finished and if you put this on an empty script specting to work it will not. Is just to show you how work the enum with switches. As i said before im not an expert just learning as anyone here. And anytime i need some script to perform some kind of only one state at the time i use this approach since is more clean that messing around with bunch of boolean values. To change the checkPStatus var, you have use something like "checkPStatus = checkpointStates.inactive", you can use the index value to but i forget how it was done since i prefer to use the propper word key to acces the variable.

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

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with Non-Linear Levels 1 Answer

Rotate character 0 Answers

Movement script preventing gravity 1 Answer

Pass a reference to a button that opened the menu this script is on 0 Answers

How do I give the player different abilities based on what color block they are standing on? 2 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