Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RaulG · Feb 11, 2015 at 12:51 AM · gameobjectgameobject.find

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HYPERSAVV · Feb 11, 2015 at 01:08 AM 0
Share

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).

avatar image RaulG · Feb 11, 2015 at 01:13 AM 0
Share

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.

avatar image mamoniem · Feb 11, 2015 at 03:47 AM 0
Share

Singleton !

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RaulG · Feb 11, 2015 at 01:30 AM 0
Share

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.

avatar image Addyarb · Feb 11, 2015 at 01:36 AM 0
Share

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).

avatar image Addyarb · Feb 11, 2015 at 01:46 AM 0
Share

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.


avatar image RaulG · Feb 11, 2015 at 01:49 AM 0
Share

Any information is amazing to get. Thank you for the help!

avatar image Addyarb · Feb 11, 2015 at 01:53 AM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RaulG · Feb 11, 2015 at 06:12 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject.Find returning NullReferenceException 4 Answers

Instantiate prefab with different values each time 4 Answers

Finding ram usage of each script 0 Answers

Need help finding the closet player to an enemy 2 Answers

check if an instantiated opject is null 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges