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 androids · Jun 11, 2013 at 10:15 PM · gameobjectobjectreplacereplacement

Replace GameObject with another GameObject

Guys, I got a problem with the replacement of an Object.

Well. My character "Obj_Char" should be change in to another Object, Object "Obj_Explosion"

If "Obj_Char" is onTrigger with a rock, then "Obje_Char should change in to "Obj_Explosion". Someone can help me out with this?

I got this for my Rock JavaScript;

 var CharExplode : GameObject;
 var Explosion : GameObject;
 
 function OnTriggerEnter(other : Collider)
 {
 if (other.gameObject.CompareTag("CharTag")) {
     CharExplode = Explosion;
 }
 }

But this doesn't work. So please help :D

Thanks in advance

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
Best Answer

Answer by robertbu · Jun 11, 2013 at 10:47 PM

I'm not sure what replacement means to you, nor have you described any parent/child relationship between these two game objects. Assuming the explosion game object is deactivated you might be what something like this:

 #pragma strict
 
 var charExplode : GameObject;
 var explosion : GameObject;
  
 function OnTriggerEnter(other : Collider) {
     if (other.gameObject.CompareTag("CharTag")) {
         charExplode.SetActive(false);
         explosion.transform.position = charExplode.transform.position;
         explosion.transform.rotation = charExplode.transform.rotation;
         explosion.SetActive(true);
     }
 }
Comment
Add comment · Show 7 · 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 androids · Jun 11, 2013 at 10:54 PM 0
Share

It says,

'position' is not a member of 'UnityEngine.GameObject'.

and

'rotation' is not a member of 'UnityEngine.GameObject'.

avatar image robertbu · Jun 11, 2013 at 10:55 PM 0
Share

Fixed. Usually I use Transform ins$$anonymous$$d of GameObject for my linked variables.

avatar image androids · Jun 11, 2013 at 11:02 PM 0
Share

I got this right now, but nothing happens

 var CharExplode : Transform;
 var Explosion : Transform;
 
 function OnTriggerEnter(other : Collider){
     if (other.gameObject.CompareTag("CharTag")) {
         Explosion.position = CharExplode.position;
         Explosion.rotation = CharExplode.rotation;
         }
 }
avatar image robertbu · Jun 11, 2013 at 11:18 PM 0
Share

Note the code I posted doesn't "replace" anything. It puts one game object at the position of another, and disables the original.

  • Put a Debug.Log() statement to verify the code is being executed.

  • Look at the position in the Hierarchy to see if both game objects exist and are at the some position.

  • $$anonymous$$ake sure that 'explosion' is enables and visible.

avatar image robertbu · Jun 11, 2013 at 11:31 PM 1
Share

SetActive is a member of a GameObject. since you rewrote your code to use Transforms, you can do:

Explosion.gameObject.SetActive(true);

Note that variable names "should" be in lower case. The compiler does not care, but most experienced folks on this list use this convention, so using upper case makes your code harder to understand.

Show more comments
avatar image
0

Answer by MRCty · Aug 05, 2014 at 09:47 AM

I also have the same need. I would replace GameObject1 with GameObject2, not just repositioning GameObject2 and deactivate GameObject1.

Assume that I have a GameObject parent and GameObject1 is a child of the parent. Then I want to switch GameObject1 with GameObject2, by code.

Is it possible?

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

16 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

Related Questions

How can I replace an object while keeping the references to that object? 2 Answers

Instantiate and destroy on given location 1 Answer

Changing multiple materials on one object? 0 Answers

Destroy the current GameObject? 7 Answers

Cannot convert object to gameObject 3 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