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 VanilliBean · Oct 05, 2018 at 05:38 PM · scripting problembooleanteleporttrue

Code isn't working properly. ( == code thing )

Heyo, I've been working on a script that whenever you'd be close to a certain prefab and press, z it'll disappear and be teleported to the bottom of the map (for saving purposes of that prefab, so no stats will be lost). Whenever you press 'p' in the script, the prefab will be teleported back to you ONLY if you've activated the 'z' part (so only if the prefab is under that certain position in the map).

The 'z' part works fine and teleports to the area fine, however the 'p' part of the script teleports whenever you press it, even if you didn't do the 'z' part first. I've tried to fix this using '==', but it doesn't really work. (Sorry if this explanation makes no sense, hopefully the script of this will clear some stuff up.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PickupObject : MonoBehaviour
 {
     public Transform Player;
     public GameObject Prefab;
     public Transform pokepos;
     public Transform Prefabpos;
 
     void OnTriggerStay(Collider collider)
     {
         if (collider.gameObject.tag == "Player")
         {
             if (Input.GetKeyDown("z"))
 
                 Prefabpos.transform.position = pokepos.transform.position;
         }
     }
     void Update()
     {
         if (Prefabpos.transform.position == pokepos.transform.position);
         {
             {
                 if (Prefabpos.transform.position == Prefab.transform.position);
 
                 if (Input.GetKeyDown("p"))
             {
                 Prefabpos.transform.position = Player.transform.position;
             }
         }
     }
 }

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

Answer by Bunny83 · Oct 05, 2018 at 06:19 PM

You have a semicolon after your two if statements. This disconnects the if statement from the code block that comes after it. An if statement always has the form:

 if (CONDITION) CONDITIONAL_STATEMENT

If you only have a single statement you want to control with the if statement you don't need curly brackets. However the "CONDITIONAL_STATEMENT" can be a complete code block when you use curly brackets. However if you put a semicolon after the if statement you terminate the statement at this point so the if statement basically has an empty statement.


Just compare your different if statements. After some you put a semicolon and after some you don't.


Finally i wouldn't recommend using a Vector3 comparison as condition. For this to work the positions have to be exactly the same. Also some of your variables are Transform references but you still use .transform on them which is completely pointless. Specifically your "Prefabpos", "pokepos" and "Player".


 void OnTriggerStay(Collider collider)
 {
     if (collider.gameObject.CompareTag("Player"))
     {
         if (Input.GetKeyDown("z"))
             Prefabpos.position = pokepos.position;
     }
 }
 void Update()
 {
     if (Prefabpos.position == pokepos.position)
     {
         if (Prefabpos.position == Prefab.transform.position && Input.GetKeyDown("p"))
         {
             Prefabpos.position = Player.position;
         }
     }
 }

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 VanilliBean · Oct 08, 2018 at 10:49 AM 0
Share

Thanks so much! Sometimes I get confused whether or not to add a semi-colon, haha

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

164 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have a object change another object's variables? 1 Answer

Boolean while looking at a game object? 1 Answer

Setting bool from other script for specific gameobjects (while they're using same movement script) 2 Answers

How to check if a audio clip had ended via scripting? 2 Answers

Ground dash is like a teleport 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