- Home /
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;
}
}
}
}
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;
}
}
}
Thanks so much! Sometimes I get confused whether or not to add a semi-colon, haha
Your answer
Follow this Question
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