Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Gohan24 · Mar 25, 2016 at 11:01 AM · variablescripting beginnerreferencing

How can I assign references to GameObjects through a script?

For context, a little info about my game. When I press a key, the character in my game (an iteration of the Player Prefab) is supposed to send out an oblong child object (absorberPrefab), after which the player prefab is destroyed.

 var absorbTransform = Instantiate (absorberPrefab) as Transform;

 absorbTransform.position = transform.position;

 Destroy (gameObject);

If this object collides with an enemy, the player gains control of the enemy by the enemy gaining the controller scripts.

 public class BasicEnemyScript : MonoBehaviour {

 void OnTriggerEnter2D (Collider2D player1Collider) {

     AbsorberScript absorber = player1Collider.gameObject.GetComponent<AbsorberScript> ();

     print ("hit");
     gameObject.AddComponent<Player1> ();
     }
 }

If it misses, another instance of the player is instantly created.

My problem is that I have a camera following script with a variable

 public Controller2D target;

which I normally have to so assign to my player gameObject via drag-and-drop in the inspector.

However, if I'm controlling new gameObjects or creating new instances of the Player prefab, I need to a way to assign the camera to follow these during runtime. How can I assign these variables during runtime?

Similarly, is there a way I can create scripts or components so that they will always take in the same variable references automatically, no matter what they're assigned to (eg. a camera script that will always target the current instance of the player prefab)?

Thank you!

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
0

Answer by meat5000 · Mar 25, 2016 at 11:01 AM

http://docs.unity3d.com/ScriptReference/GameObject.html

Under 'Static Functions'

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 Gohan24 · Mar 26, 2016 at 02:08 AM 0
Share

Thanks for the reply! In order to send out the oblong prefab, the script that controls it needs to take in a reference to a prefab, in this case called "Absorber1". When I try

  public GameObject absorber1Prefab;
 void Awake () {
 absorber1Prefab = GameObject.Find     ("Absorber1");
 }

 void Update () {
 Instantiate (absorber1Prefab,     gameObject.transform.position,     gameObject.transform.rotation);
 }


While this compiles perfectly well, at runtime the prefab is not assigned and the field in the inspector returns "none".

Also, I realized the above Camera Follow script takes a class (Controller2D) as a reference, not a gameObject. Is there code that will let me automatically assign this? Thank you!

avatar image
0

Answer by Keita-kun · Dec 27, 2018 at 02:05 PM

Hi there, I may be late but just in case someone has same issue. my solution comes in two parts

1- you can use a Scriptable Object to hold a reference to the prefab i e: public GameObject[] myPrefabs; then assign the values to the list on the inspector this way you'll have a persistent refrence to the prefabs so instantiate won't return a null object when spawning the absorber nor the player.

2- while you do need a reference to the prefab, you also need to set the the reference to the camera's property "`public Controller2D target`;" given that you're player spawns correctly you can attach its transform to the camera like this:

 //assign back a ref to the player prefab
     Player player1 = Instantiate (myScriptableobject.myPrefabs[0],     gameObject.transform.position,     gameObject.transform.rotation);
      
     //find the camera or or create another property that hold the ref to the camera
     Camera mycam =  Instantiate (myScriptableobject.myPrefabs[1], myScriptableobject.myPrefabs[1].transform.position, myScriptableobject.myPrefabs[1].transform.rotation) ;
     mycam.target = player1.target;
     //assuming the target is a variable on the playerScript or a property



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

explain me the script 0 Answers

Access scripts on Awake or Start? 1 Answer

How to make GUI Text show number from another script 2 Answers

I am trying to reference a bool from another script but the only value that it returns is false 0 Answers

Make a Scene variable 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