Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 carter-carl30 · Jul 29, 2015 at 07:44 PM · instantiatetransformprefabchild

Spawn Shield to all objects

Hi all, I have 3 objects in my game (that move around randomly). What I am trying to do is when you buy the shield upgrade in the shop it adds a shield to each one.

My script so far adds the shields at each objects current position when you buy it in the shop but then the shields just stay where they instantiated not stayed attached to the objects, can anyone help to make each shield object then follow the object it spawned on?

 private var ray : Ray;
 private var rayCastHit : RaycastHit;
 var buySound : AudioClip;
 var buy_effect : GameObject;        //buy effect
 var tick : GameObject;
 var locked : GameObject;
 var plus2 : GameObject;
 var player : GameObject;        //The player
 public var respawnPrefab: GameObject;
 public var respawns: GameObject[];
 
 function Start(){
 
         //Find jiji_standard
         //jiji = GameObject.Find("jiji_standard");
         
         //Find jiji_standard 1
         //jiji1 = GameObject.Find("jiji_standard 1");
 
         //Find jiji_standard 2
         //jiji2 = GameObject.Find("jiji_standard 2");
 
         //Find player
         player = GameObject.Find("Player");
         
         tick.active = false;
 }
 
 
 function Update(){
 
 if(Input.GetMouseButtonDown(0)){
 
 ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
 if (Physics.Raycast (ray, rayCastHit)){
 
 if(rayCastHit.transform.name == "shield" && locked.active == false){
 
 //crate buy effect at point of hit
 buy_effect.active = true;
 
 audio.PlayOneShot(buySound);
 
 tick.active = true;
 
 player.GetComponent(Game5_Player).Addmana(-80);
 
 plus2.collider.enabled = false; 
     
         respawns = GameObject.FindGameObjectsWithTag("jiji_standard");
     for (var respawn: GameObject in respawns) {
 
         Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
         
 
 }
 }
 }
 }
 }


Comment
Add comment · Show 4
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 Positive7 · Jul 29, 2015 at 07:52 PM 0
Share

Does the shield a child of the object?

avatar image carter-carl30 · Jul 31, 2015 at 05:14 PM 0
Share

the shield is a prefab

avatar image carter-carl30 · Aug 01, 2015 at 12:14 PM 0
Share

I changed what you put to:

  var instance = Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
  instance.transform.SetParent(respawn.transform);

and it works perfect, thankyou very much!! was driving me mad :)

how do i mark this comment with a tick?

avatar image ShadyProductions · Aug 01, 2015 at 03:16 PM 0
Share

you can now :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ShadyProductions · Jul 31, 2015 at 05:20 PM

 GameObject instance = Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
 
 instance.transform.SetParent(respawn.transform);

something like this?

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 Ellenack · Jul 29, 2015 at 09:47 PM

You are probably looking for this :

http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

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 carter-carl30 · Jul 31, 2015 at 05:19 PM 0
Share

I tried adding : respawnPrefab.transform.SetParent(respawn);

under the line Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);

but i get this error:

Assets/Game 5/Script/Javascript/buy_shield.js(55,50): BCE0023: No appropriate version of 'UnityEngine.Transform.SetParent' for the argument list '(UnityEngine.GameObject)' was found.

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

23 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

Related Questions

Instantiate a Prefab as child 0 Answers

Variables stay overwritten in inspector 1 Answer

Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer

Passing a variable to the child of Instantiated object? 1 Answer

Prefab as child of existing GameObject 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