- Home /
Find, Findwithtag, findgameobjectwithtag Not working
Hi, my unity 2018.2.14f1 not finding game object, i dont know whats wrong with it, Code is below i have tried it with FindWithTag,FindGameObjectWithTag too, this prefab has name Ball and the tag too, whats wrong with it? can anyone please help me. if I make it public, and drop prefab manually, it works fine, but not finding it.
public class Projectile : MonoBehaviour
{
public Transform target;
public Transform throwPoint;
private GameObject FireBall;
public float timeTillHit = 1f;
public Slider AngleSlider;
void Start ()
{
FireBall = GameObject.Find ("Ball");
}
public void FireTheBall ()
{
float xdistance;
xdistance = target.position.x - throwPoint.position.x;
float ydistance;
ydistance = target.position.y - throwPoint.position.y;
float throwAngle; // in radian
//OLD
//throwAngle = Mathf.Atan ((ydistance + 4.905f) / xdistance);
//UPDATED
throwAngle = Mathf.Atan ((ydistance + 4.905f * (timeTillHit * timeTillHit)) / xdistance);
//OLD
//float totalVelo = xdistance / Mathf.Cos(throwAngle) ;
//UPDATED
float totalValue = xdistance / (Mathf.Cos (throwAngle) * timeTillHit);
float xValue, yValue;
xValue = totalValue * Mathf.Cos (throwAngle);
yValue = totalValue * Mathf.Sin (throwAngle);
if (FireBall == null) {
FireBall = GameObject.Find ("Ball");
}
GameObject bulletInstance = Instantiate (FireBall, throwPoint.position, Quaternion.Euler (new Vector3 (0, 0, 0))) as GameObject;
Rigidbody2D rb;
rb = bulletInstance.GetComponent<Rigidbody2D> ();
rb.velocity = new Vector2 (xValue, yValue);
}
public void MoveTarget ()
{
target.position = new Vector3 (0f, (float)AngleSlider.value);
}
}
//END-Class
Answer by xainulabdeen · Nov 19, 2018 at 12:25 PM
hello !
1. GameObject.FindGameObjectWithTag("Tag")
only works if your gameobject is active in the screen make sure your game object is active otherwise this function will not work.
Answer by Legend_Bacon · Nov 19, 2018 at 12:21 PM
Hello there,
First, please do NOT post the same question 10 times. It makes you look very unprofessional, and can lead to sanctions as that could be considered spamming. I've removed the other questions this time around.
Second, your problem is that you're trying to find a Prefab instead of an instance of it.
You cannot find a prefab by name with GameObject.Find(), as it only looks in your scene.
Either you make it public (or SerializeField it) and assign it, or you put it under a Resources folder and load it using Resources.Load().
I hope that helps!
Cheers,
~LegendBacon
Answer by BeefyMcWhatnow · Mar 03, 2021 at 12:04 PM
"your problem is that you're trying to find a Prefab instead of an instance of it.
You cannot find a prefab by name with GameObject.Find(), as it only looks in your scene. Either you make it public (or SerializeField it) and assign it, or you put it under a Resources folder and load it using Resources.Load()."
Or you could just unpack the prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182400im_/https://answers.unity.com/themes/thub/images/avi.jpg)