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 Aidz · Apr 04, 2015 at 10:34 AM · c#prefabawakeprefab changing at runtime

How to attach a Script/GameObject in the Scene view to a Prefab in the Assets folder.

I have a Bullet prefab which has a Gravity Script attached to it, it requires a Planet GameObject to be plugged in.

alt text

When the game is started with the bullet in the scene (not in the resources folder) Attractor has a the planet attached to it, which looks like this.

alt text

The Planet object is a gameobject in the scene view, however when the prefab is in the resources folder (Where I need it) it won't allow me to plug in the Planet.

Planet has a Script called GravityAttractScript to it, and I think that is what I need to access, however I will need the Planet Gameobjects transform for the script to work.

My question is, how do I either get the Planet to plug in to the GravityBodyScript while it is in the resources folder, or how do I get to plug the planet when the bullet is instantiated?

Here is the relevant script for GravityBodyScript that I want to have the planet properties attach to.

 public GravityAttractScript planetAttractor;
 private GameObject planetObject;
 
 void Start()
 {
   originTransform = GameObject.Find("OriginLimiter");
   planetObject = GameObject.Find("Planet");
 }


So to reiterate, this code works when the bullet is already in the Hierarchy/Scene View before I start the game, but not when the game is instantiated from a prefab, I tried changing the code to Awake() instead of Start(), and also including Awake(), but that field never gets the information.

prefab.jpg (10.8 kB)
sceneview.jpg (11.0 kB)
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
1
Best Answer

Answer by hbalint1 · Apr 04, 2015 at 11:43 AM

When you instantiate the object it has some specific name like: Planet(Clone). Try to change the Find method to:

 planetObject = GameObject.Find("Planet(Clone)");

I'm not sure why you can't drag and drop from the Resources. Change private to public. After that you should be able to drag and drop it.

Comment
Add comment · Show 3 · 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 Aidz · Apr 04, 2015 at 12:51 PM 0
Share

The planet object is already in the scene view and isn't being instantiated or cloned, I'm only instantiating the bullet.

So I do have lots of Bullet(Clone) objects, but none of them have the planet plugged in.

Here is the code for the bullet being instantiated.

 //This is where the bullet object is plugged in
 public Rigidbody bulletRigidbody;

   void ShipFiring()
     {     
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             Rigidbody bulletClone;
 //Instantiate bulletRigidbody in front of object ($$anonymous$$aybe I could set the bullet clones planetObject here somehow? Not sure how to do that)
             bulletClone = Instantiate(bulletRigidbody, gunPlaceholder.position, transform.rotation) as Rigidbody;
 
 
 //$$anonymous$$ove Bullet Forward
             bulletClone.velocity = transform.TransformDirection(new Vector3(0,0,bulletSpeed));
 
 //Destroy the bullet after a few seconds
             Timer(bulletClone);
         }
     }




avatar image hbalint1 · Apr 04, 2015 at 01:11 PM 0
Share

so if you have only one planet why do you want to set it like this? planetObject = GameObject.Find("Planet");

If you have the prefab at the Resources, just set the private GameObject planetObject; to public and drag and drop the planetObject from the scene to the prefabs planetObject. So it will automatically get it right after instantiate and you don't need to Find() it.

avatar image Aidz · Apr 04, 2015 at 02:26 PM 0
Share

I ended up using a getcomponent to get the script from the planet that I needed, and I set the planet object to public just to make it easier, and now it is working, for those who want to know how I did it, here is the code.

 planetAttractor = planetObject.GetComponent<GravityAttractScript>();

So turns out you can't just try and plug the planet object into the GravityAttractScript socket, this method at least allows you to access the object and script.

Thanks for the help man!

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Prefabs spawn with wrong values 0 Answers

Multiple Cars not working 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

Animator is not playing a Playable 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