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 DMCH · Feb 19, 2013 at 03:47 PM · c#replacement

Is it possible to replace the script on a gameobject with another of the same type?

Hello,

I'm trying to replace the script of an instantiated gameobject with one of the same type I have stored in a List. Here's the code, which is throwing an error on the final instruction.

The error is: The object of type turret gun scripthas been destroyed but you are still trying to access it

Is this sort of script replacement possible? And if so, is it a good idea?(i.e. won't cause unexpected behaviour) Thanks for your time!

             // Instantiate and get reference to Gameobject
             GameObject tempTurret = (GameObject)Instantiate(turretPrefab2, turretScriptList[i].GetTurretPosition(), Quaternion.identity);
         
             if(tempTurret == null)
             {
                 Debug.Log("TurretSceneScript1.PlaceExistingTurrets: Turret1 is null");    
             }
             
             // Get the current turret's script
             TurretGunScript currentTurretScript = (TurretGunScript) tempTurret.transform.Find("Gun").GetComponent("TurretGunScript");
             
             // Get the stored Script in tempsave
             TurretGunScript replacementScript = (TurretGunScript) turretScriptList[i];
             
             // Replace the current script
             currentTurretScript = replacementScript;
             
             // Set the texture appropriate to that type
             currentTurretScript.SetTurretTexture();
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
Best Answer

Answer by 1337GameDev · Feb 19, 2013 at 03:52 PM

When you destroy the component or remove , references to it dont work. So storing a reference to it after it is deleted will return null. The reference just indexes its memory address, when you delete it, the object is considered "deleted" in memory and not allowed to be accessed. Components normally have non-static members anyways and would cause more problems.

Use the add component method to add a script at runtime. You can tore references to scripts as objects, but if it was attached to a gameObject, I believe you have to attach it first, and then update the reference to it.

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 DMCH · Feb 19, 2013 at 05:12 PM 0
Share

Hello again. Thanks a lot for your help!

avatar image
1

Answer by Owen-Reynolds · Feb 19, 2013 at 04:57 PM

The net effect would be the old script's global variables are replaced by the globals in the new one?

Seems like it might be easier to write a small routine to copy the variables you need (like the gun type and ammo prefab?)

Comment
Add comment · Show 2 · 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 1337GameDev · Feb 19, 2013 at 05:29 PM 0
Share

Yeah, that's how you would do it the easiest. You would need a script that stores Hesse values and then removes the script you want from your gameObject and then uses AddComponent to add a new script and copy values over.

avatar image DMCH · Feb 19, 2013 at 05:31 PM 0
Share

Yep, Owen, you're right on this one. Got it working (kinda) by removing and adding a new script but there were a lot of errors (broken references and others). I ended up just writing a method to copy what is needed, and it worked off the bat. Thanks for the suggestion!

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System. 0 Answers

Can't transfer colllider from OnTriggerEnter to a public bool 1 Answer

A problem with arrays 1 Answer


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