- Home /
I can only select a prefab as my GameObject in the editor.
I wrote two scripts and made a public GameObject in both to use the game object and select it in the editor. It worked in one script but not in the other. One script will allow me to select a prefab or a gameobject, while the other will only let me use a prefab.
This is the script that won't let me use my non-prefab gameobject:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemysProjectile : MonoBehaviour
{ public float speed; public float lifetime; public float distance; public LayerMask whatIsSolid; public float damage;
public LayerMask whatIsPlayer;
public GameObject player;
void Start()
{
Invoke("DestroyProjectile", lifetime);
}
void Update()
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
RaycastHit2D ifHitPlayer = Physics2D.Raycast(transform.position, transform.up, distance, whatIsPlayer);
if (hitInfo.collider != null && hitInfo.collider.CompareTag("Enemy") == false)
{
DestroyProjectile();
}
if (ifHitPlayer.collider != null && ifHitPlayer.collider.CompareTag("Player") == true)
{
DestroyProjectile();
player.GetComponent<HealthBar>().healthBar(damage);
ifHitPlayer.collider.GetComponent<PlayerProperties>().DamageToPlayer(damage);
}
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
The other script which it worked in is below:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HealthBar : MonoBehaviour { public GameObject slider; public float playerHealth;
public GameObject player;
void Start()
{
playerHealth = player.GetComponent<PlayerProperties>().playerHealth;
}
void Update()
{
}
public void healthBar(float damage)
{
damage /= 100;
damage /= playerHealth;
slider.transform.localScale -= new Vector3(damage, 0, 0);
//get new source for coed above; change setsize
}
}
Answer by revolute · Nov 05, 2019 at 07:30 AM
If you can only select prefab, I must assume its due to the fact that the script is attached to a prefab itself. GameObjects in scene can select other gameobjects in the same scene or prefabs under "Assets" folder. Prefabs cannot hold reference to gameobjects in the scene.
Your answer
Follow this Question
Related Questions
How do I make a progress bar in the editor lock the background? 0 Answers
Animator Window Layout Problem 0 Answers
Build problem 1 Answer
Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer
Unity Editor not showing my partial class in inspector when i try to assign field() 0 Answers