- Home /
Need an alternate to Gameobject.Find
using UnityEngine;
using System.Collections;
public class bulletLas : MonoBehaviour {
public GameObject circle;
private Vector3 circleTrans;
void Start()
{
circle = GameObject.Find ("Blaster");
}
void Update ()
{
Transform B = transform;
Vector3 newPosition = B.position - circle.transform.position;
Quaternion lookRotation = Quaternion.LookRotation(newPosition);
B.rotation = Quaternion.Slerp(B.rotation, lookRotation, Time.deltaTime * 5.0f);
transform.position += newPosition * Time.deltaTime;
Destroy (gameObject, 2);
}
}
For some reason, if I attempt to connect the object to a public GameObject (circle), it does not update the transforms.
Im guessing it's because Im connecting a prefab to the circle, and it's using the prefabs transforms, not the object in space. Which is why Gameobject.Find works, since it takes the object from space.
So I would need something that isn't as resource heavy to find the object in space to connect it properly to the prefab.
If you're just using it in start it should be fine performance-wise. If you're creating thousands of these bullets at a time, and assu$$anonymous$$g this bullet is spawned by the blaster, you could set circle from the instantiating script since it's public. That'll reduce the calls for Find("Blaster") to one (or zero if the script is attached to the "Blaster" GO).
I just found out that if two objects of the same name exist, that also causes some problems..
Although I did put it into the start function of the Blaster GO.
Answer by Addyarb · Feb 11, 2015 at 01:18 AM
Make a public reference to the object and save it as a prefab.
Tag the item and use GameObject.FindWithTag
Make a script that holds an ID (if the above methods don't get the one you want due to uniqueness) and tag it as "Circle" or something. Then use a for, or foreach loop to iterate through and find that particular ID.
Hope this helps, if not give me more info on exactly what you're trying to do and I'll try my best!
Addyarb
if possible could you show an example script of Id?
Umm to answer both at the same time, it's a spawning script that spawns the Blaster GO, and the Blaster GO also has a spawn script that "fires bullets".
I could just connect the prefab to the inspector of the Bullet, but for some reason it takes the transforms of the prefab, not the object in space.
Using Gameobject.Find finds the last object that was instantiated, so it takes those transforms and uses it for all objects under the same name, Im going to assume it will do the same with Tag.
Although the ID thing sounds promising. I've never used them before so I'm more then excited to know how to implement it.
I hope that answers the question of what i'm trying to do. If not I'll try to be more specific.
So let me see if I understand
You are firing a projectile with a firing script. Then your projectile has a script attached to it that handles it's speed, direction, and destruction, right?
If that's the case, instantiating the object and keeping track of it will be fairly easy by "passing" a value into a function where you are instantiating it in the first place. That sounds kind of intimidating, but it's really not.
Actually, the set of tutorials where I first learned how to deal with such things is a very valuable resource. Perhaps you can check it out? http://www.gamertogamedeveloper.com/gtgd-series-1/video-3
I know that video may be a little longer than you're used to, but you can download his scripts for that video in the downloads section. (Just download the zip and look in "Video 3" folder).
However, to answer your question about the ID, here's how I'd work it.
//This is the ID script..
public int myID;
void Start(){
gameObject.tag == "Bullet";
}
And here's the firing script
public GameObject projectilePrefab;
public int projectileIDIJustFired = 0;
void Update(){
if(Input.GetButtonDown("Fire1")){
FireBullet(ID);
ID +=ID;
}
void FireBullet(float ID){
GameObject bullet = Instantiate (projectilePrefab) as GameObject;
bullet.GetComponent<projectileIDScript>().ID = ID;
}
}
Haven't tested that as I just typed it in here, but this is more just for general understanding of how it'd work. The link I linked has a better system.
Of course :) With that spirit you'll have it working in no time. If you don't get it at all that's okay too, just keep asking questions. That's what the forum's for.
Answer by Stardog · Feb 11, 2015 at 01:23 AM
Your "circle" is a prefab, but the prefab isn't in the scene. You will have to Instantiate it (copy it to the scene) first.
public GameObject circlePrefab;
public GameObject circleInstance;
void Start()
{
circleInstance = (GameObject)Instantiate(circlePrefab, transform.position, Quaternion.identity);
}
Using Find in Awake/Start is fine for performance (unless you are instantiating this script a lot at runtime), but not so in Update.
You could use Tags as an alternate to Find. Or just drag into the inspector. I almost never use GameObject.Find.
You will have to explain what you're trying to do.
The object is instantiated as a clone, and name changed to Circle. Which is then found with GameObject.Find.
If I tried to get the values from the instantiated object, it would still use the prefab transforms.