• 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
1
Question by Tokyoscarab · Feb 07, 2017 at 05:42 PM · gameobjectprefabsreferencing

How do you reference a gameobject in a script on a prefab?

I have a 2d game that I'm messing with the perspective of objects. For example, being able to walk infront of and behind certain things like trees, fences, etc and having the layers sorting to that perspective. I have some code written that does that, but my problem is that since the game is randomly generated, the script is on prefabs, but, for now, my character is an object in my scene. Unity won't let me drag and drop that as the reference. So how can I go about that in script?

 public GameObject DOGE;
 private Renderer SR;

     if(DOGE.transform.position.y < (gameObject.transform.position.y - 0.5f))
     {
         SR.sortingOrder = 0;
     }
     else
     {
         SR.sortingOrder = 5;
     }


   Because the game is randomly generated, I will probably have to have other prefabs detect other prefabs for things like collisions or other various interactions, any help into this would be greatly appreciated! :D
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by game4444 · Feb 08, 2017 at 09:27 AM

Give tag Player to your gameObject player. In start function void Start() { DOGE = GameObject.findGameObjectWithTag("Player"); } it will assign your player to DoGe. Hope it will work.

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
avatar image
0

Answer by James2Games · Feb 07, 2017 at 09:20 PM

It seems very odd that you can't drag and drop an object from your hierarchy onto the DOGE field as if it's in the hierarchy then it will be of type GameObject.

If you want to do it via script and have a reference somewhere else in your code you can do this.

 DOGE = Player.Instance.gameobject

Using this as an example

 public class Player : MonoBehaviour
 {
     public static Player Instance;
     void Start()
     {
         Instance = this;
     }
 }



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 Tokyoscarab · Feb 08, 2017 at 05:31 AM 0
Share

Thanks! I changed a couple things around and it's working but I'm running into another problem. Each fence piece generated either seems to be above or under me on 1 point on the Y axis and not dependent on each piece. Why would that be? I'm running the same part as above, but changed "gameObject" to "this" ins$$anonymous$$d, same problem.

avatar image James2Games Tokyoscarab · Feb 08, 2017 at 05:39 AM 0
Share

'this' and 'gameObject' are two different things. 'this' in the Player class above represents the Player script. Where as 'gameObject' represents the gameObject that is holding the Player script.

Can you share you code as well as images showing it it looks like in the scene?

avatar image Tokyoscarab · Feb 08, 2017 at 05:49 AM 0
Share

I had only tried switching it to "this" just to see if would make any difference at all. When the game starts, a map is generated with various land tiles, then some assorted fence tiles are generated. So I want each of those to be able to change their sorting based on the player character's position relative to a specific point on that tile. I assumed that having this on a prefab for that tile would have that apply to every copy. https://gyazo.com/7d8fc5f30bf564678720d3ca57de373d

avatar image James2Games Tokyoscarab · Feb 08, 2017 at 06:36 AM 0
Share
 if(DOG$$anonymous$$transform.position.y < (gameObject.transform.position.y - 0.5f))

The script looks like it's doing as it's being told according to the gif you sent. The reason why the topleft fence is behind the dog is because of

 gameObject.transform.position.y - 0.5f

If the dog and the fence are both positioned on a y of 0.0 then the fence will be moved down by 0.5 then they will both be compared. Hense the fence will be drawn behind the dog.

Perhaps try + 0.5f?

avatar image Tokyoscarab James2Games · Feb 08, 2017 at 04:56 PM 0
Share

Ok, so after putting a console command to write the DOGE location every frame, I've noticed that it's only ever giving the same position regardless of me moving the character or not. In fact, it seems the data it's writing is the default location on the prefab and not the actual object in game. I guess my question is then, how do you actively keep track of an object's position? I assumed once I had a reference to the object, all the data of said object would refresh with the reference.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

[Probably an easy question]Troubles saving my GameObject through scenes 1 Answer

How can i set a prefab to x=0 and y=0. 3 Answers

Destroy all Objects of a Type in Game 1 Answer

Does not contain definition 1 Answer

GameObject references runtime script in scene file. Unsure whats wrong 2 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