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 Carnivorous · Apr 27, 2015 at 02:04 PM · prefabclonewtf

Script finds a non-existing object

I have a really weird problem with my game. I'm using GameObject.FindWithTag in the Update() to find the player transform for my camera script. This is obviously very basic stuff. But now, for some reason, the script finds an object called Player(clone), that doesn't exist in the current scene. Every other script and object in the scene works fine. If I manually drag the player in inspector view, it works fine. If I double click the transform variable in the script that currently is Player(clone), it shows an animator controller in the inspector view and no other components. It just doesn't make any sense. The name Player(clone) would indicate that it would be an object created using Instantiate, but I haven't used it at all. I have no idea how to fix this.

Also, I can't believe that "wtf" was an actual approved suggested tag.

Comment
Add comment · Show 2
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 smallbit · Apr 28, 2015 at 10:15 AM 0
Share

Using gameObject.Find... in Update is one of the biggest sins of a unity developer. Consider storing the reference somewhere (i.e. drag into inspactor. or reference through Singleton(i.e. player.instance.gameobject). It will not answer to your question but will fix your problem.

avatar image dudester · Apr 28, 2015 at 10:23 AM 0
Share

found this myself , what i did was look for a specific script on the gameobject then using findObjectOftype().transform if you want the transform or findObjectOftype().gameobject for the gameobject not exact code so look up the proper references but those should work seems to be a bug in unity 5.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brunopava · Apr 28, 2015 at 12:02 PM

Personaly, I like to make a Singleton and cache some importante variables in it like player, points, list of enemies and etc. The upside is that you make sure that there will be only one instance of the Singleton in your scene, therefore you will not have this problem of accessing other instances instead of the one you want.

The code for a Singleton is:

 using UnityEngine;
 using System.Collections;
 
 public class SingletonExample : MonoBehaviour
 {
     private static SingletonExample _instance;
 
     public static SingletonExample instance
     {
         get{
             if(_instance == null)
             {
                 GameObject singletonHolder = new GameObject("[SingletonExample]");
                 _instance= singletonHolder.AddComponent<SingletonExample>();
             }
             return _instance;
         }
     }
     
     public void Awake()
     {
              //ONLY IF YOU WANT IT TO NOT EVER BE DESTROYED
          DontDestroyOnLoad(gameObject);
     }
 }


And you call like this:

SingletonExample.instance.someVariableYouCreated.ActionYouWantItToTake();

Or in your case:

SingletonExample.instance.player instead of GameObject.FindWithTag("Player");

Dont forget to declare your variables and assign them onto the Singleton.

If you want to learn more about check this link: http://en.wikipedia.org/wiki/Singleton_pattern

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array of prefabs 0 Answers

Cast Source Instantiating Error 1 Answer

How to control variable of prefab and its clone 0 Answers

How to change sprite on a cloned object? 1 Answer

Unity 2D : How to make a perfect clone of my Game Object? 3 Answers


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