Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Cosmology27 · Feb 02, 2020 at 12:06 AM · gameobjectvariable

Public variable from serialized field for prefab

I have a prefab for a bullet the enemies shoot. I have the bullet's path dictated by a script on the bullet (bulletpath). Basically, it looks at the position of the bullet when it's instantiated, and the position of the player, and forms a vector3 to make the velocity, so the bullet will fly at the player. This works extremely well, if the player is alive. When the player is dead, bulletpath obviously has no reference for what object to plug into its math, so I get flooded with errors, which makes sense. What I want to do is make an if statement to check if the player is alive, if so, then the math plugs the player position in. If the player isn't active, then it will use a cube I've made past the starting position, and basically aim at that. All of this math makes sense for me and what I'm doing, it's just I'm having a problem getting the prefab to identify whether the player object is active.

My question is this, I want to make a [SerializedField] of a gameobject in the bulletpath script, which is attached to the bullet prefab. However, when going into my prefab to edit it, I no longer have access to the main hierarchy, so I cannot drag my player object into the serializedfield of my bulletpath script on my bullet prefab.

I've tried creating a new variable in another random script outside the prefab that will identify the player, then I could call on that within the prefab. The problem is, if it's no "static public" then I cannot access it, and if it is "static public" then I can't use the serializedfield to identify which game object I want it to be...

I've also tried using "FindObjectOfType()" to find my player object (ShipControls is a script that only my player object has on it), but it won't allow me to do this for telling a gameobject variable what gameobject it references (even though it allows me to do this for a gameobject's transform values, which makes no sense to me). It says I cannot implicitly convert type 'ShipControls' to 'UnityEngine.GameObject'. That makes no sense to me, because it should find the thing that has 'ShipControls', which is a GameObject, so I definitely can use a GameObject to be a GameObject.

I'm getting very confused the more I try to figure out a way to do this. Basically, I want to know how to either. 1) Plug in a value for a serializedfield in a prefab from the main hierarchy. or 2) Reference a gameobject purely using script.

I guess? Thanks for any help you can provide. (Also, why does this site totally ruin the formatting of what I type? It ignores all the times I pushed enter...?)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DreivanBG · Feb 02, 2020 at 12:26 AM

Hi, I'm new to Unity but i think i see your error when you try to use FindObjectOfType() .

ShipControls is not GameObject, if you want to access his methods you need to do something like this:

ShipControls playerShipControls = FindObjectOfType();

After that you can access all public methods my just typing playerShipControls.publicMethodName

I hope that will help you a little bit good luck.

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

263 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 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 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 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 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 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 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 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 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 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 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 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

I need to activate a function in another script from a script on a different game object 0 Answers

How to change value of another gameobject through script 2 Answers

Why does Visual Studio 2019 doesn't show the list of next possible codes and doesn't show in green variables like Sprite or Image? 0 Answers

Enemy Attacking Even When Not In Range 2 Answers

Variable not being assigned 0 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