- Home /
Question by
mesutcankaptan · Sep 29, 2016 at 01:01 PM ·
c#drawer
I want to move my gameobject on z axis. I have a problem with my script. Please help! C#
Hello everyone.. I have a drawer script.. Actually it was door script. I tried to change it for open/close drawer. I use this script with another Interact script (i'ts working i tried) . This is my script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Drawer : MonoBehaviour {
public bool open = false;
public float speed = 2f;
private AudioSource audioSource;
public AudioClip openingSound;
public AudioClip lockedDrawerSound;
public bool isLocked = false;
public GameObject lockedText;
public float textTime = 4f;
public Transform target;
void Start()
{
audioSource = GetComponent<AudioSource> ();
lockedText.SetActive (false);
}
public void ChangeDrawerState()
{
if (isLocked != true) {
open = !open;
if (audioSource != null) {
audioSource.PlayOneShot (openingSound);
}
}
else
{
StartCoroutine(LockedDrawer());
lockedText.SetActive (true);
}
}
IEnumerator LockedDrawer()
{
audioSource.PlayOneShot (lockedDrawerSound);
yield return new WaitForSeconds (textTime);
lockedText.SetActive (false);
}
void Update ()
{
if(open)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
else
{
float step = speed * Time.deltaTime;
target.position = Vector3.MoveTowards(target.position, transform.position, step);
}
}
}
there is no error but it's not opening or closing drawer too... What should i do? What is wrong in this script? i am not good at this "Vector3" and "transform" things.. Please help me :) Thanks
Comment
Answer by WinterT101 · Sep 29, 2016 at 09:33 PM
@mesutcankaptan On line 64 you have target.position
try changing that to transform.position
Your answer
![](https://koobas.hobune.stream/wayback/20220612093633im_/https://answers.unity.com/themes/thub/images/avi.jpg)