- Home /
Object assigment disappears in inspector
Hi, I'm new to Unity and I have a problem, so i have a script that allows the player to pick a cube up basically what happens is that the character presses E and then the box collider of the object turns off so i can grab it without the box collider of the player and the object forced to be pushed inside each other:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Grab : MonoBehaviour {
public bool grabbed;
RaycastHit2D hit;
public float distance=2f;
public Transform holdpoint;
[SerializeField]
private BoxCollider2D collider;
[SerializeField]
private SpriteRenderer Armright;
[SerializeField]
private SpriteRenderer Armleft;
public SpriteMask Maskleft;
public SpriteMask Maskright;
// Start is called before the first frame update
void Start()
{
collider = GetComponent<BoxCollider2D>();
Armleft = GetComponent<SpriteRenderer>();
Armright = GetComponent<SpriteRenderer>();
Maskright = GetComponent<SpriteMask>();
Maskleft = GetComponent<SpriteMask>();
}
// Update is called once per frame
void Update() {
if(Input.GetKeyDown(KeyCode.E))
{
if(!grabbed)
{
Physics2D.queriesStartInColliders=false;
hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x,distance);
if(hit.collider!=null)
{
grabbed=true;
}if(grabbed)
{
collider.enabled = !collider.enabled;
Armleft.enabled = !Armleft.enabled;
Armright.enabled = !Armright.enabled;
Maskright.enabled = !Maskright.enabled;
Maskleft.enabled = !Maskleft.enabled;
}
}
}else
{
}
if(grabbed)
{ hit.collider.gameObject.transform.position = holdpoint.position;
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position,transform.position+Vector3.right*transform.localScale.x*distance);
}
} }
but everytime when I drag a gameobject to the script. it disappears when i hit play. i've tried to find anwers for this but i couldn't find a solution. does anybody know why this happens? hope i'm clear.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
animation problem 1 Answer
2D Sprite constantly flipping 0 Answers
Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer
Super Mario Bros 2D very lagging 1 Answer
scripting problem 0 Answers