My gameobject disappears when I transform it
I am new to unity and programming in general, so excuse me if this is a simple question. I am trying to get a gameobject become twice as large and move to the player's position once when the player interacts with it. However, whenever it moves the gameobject, it disappears from the game but I can still see it in the scene view. Any help would be appreciated. Below is my code and a screenshot of what I described.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupScript : MonoBehaviour {
Transform camerapos;
Transform size;
int scaled = 0;
public GameObject cam;
void Start()
{
size = GetComponent<Transform> ();
camerapos = cam.GetComponent<Transform> ();
}
void OnTriggerEnter2D(Collider2D col) {
if (globalvar.interact) {
if (scaled == 0) {
size.localScale = new Vector2 (2, 2);
size.position = camerapos.position;
scaled = 1;
}
}
}
void OnTriggerStay2D(Collider2D col) {
if (globalvar.interact) {
if (scaled == 0) {
size.localScale = new Vector2 (2, 2);
size.position = camerapos.position;
scaled = 1;
}
}
}
}
Answer by saud_ahmed020 · Apr 11, 2017 at 05:35 AM
@FlyingAmigos you are changing the world position of gameobject. Just change the local Position not position.
I tried it just now, it did not work. The same result happens, where it is the correct scale and position in the scene view but is invisible in the game view.
@FlyingAmigos This is 2D game. Whe you change the position then the camera position (z value) also changed and camera goes forward. camera must be behind the scene object. So now just change the x and y value of camera and do not change the z value.
Thank you for the answer. I feel dumb now.
Your answer
Follow this Question
Related Questions
How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer
Get the transform.position of a gameobject in an array? 2 Answers
How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer
Giving Instantiated Clones their own properties and variables 1 Answer
Detect overlapping objects 2D game 0 Answers