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 Pixelen · Nov 10, 2012 at 09:48 PM · c#instantiatedestroyscriping

Show/hide object in C#

Hi all, I have recently moved into C# scripting from JavaScript and I would like to know if there is a way of controlling an object in C# so that it will be hidden when the scene starts but will then appear when a collision occurs? This is the script I currently have:

 using UnityEngine;
 using System.Collections;
 
 public class collideEnemy : MonoBehaviour {
     
 void Start () {
     Destroy(GameObject.Find("Cog"));
 }
 
 void OnTriggerEnter(Collider other) {
     Destroy(GameObject.Find("Enemy"), 1);
     Instantiate(GameObject.Find("Cog"));
 }
 }

I was trying to accomplish what I wanted to do with the destroy and instantiate functions, however this gives me a "NullReferenceException" error, I'm guessing because the item I'm trying to Instantiate is already destroyed. Is there another way to do this, or a way around this? I was thinking of calling the function from another script but I'm unsure how to do this in C# or if this would even work. I'd appreciate any help!

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 whydoidoit · Nov 10, 2012 at 10:02 PM

I don't think calling it from another script would help. Presumably you actually need Cog to be a prefab - then have a variable of type GameObject in your class that you assign to the Cog prefab using the inspector.

     GameObject cogPrefab;
    ...
 
    Instantiate(cogPrefab, other.transform.position, Quaternion.identity);

To create the prefab just drag the game object from the hierarchy to the project view.

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 Pixelen · Nov 10, 2012 at 10:15 PM 0
Share

Thanks for the help. I made the cog a prefab and named it cogPrefab, and changed my script to this:

using UnityEngine; using System.Collections;

public class collideEnemy : $$anonymous$$onoBehaviour { GameObject cogPrefab;

void Start () { Destroy(GameObject.Find("cogPrefab")); }

void OnTriggerEnter(Collider other) { Destroy(GameObject.Find("Enemy")); Instantiate(cogPrefab, other.transform.position, Quaternion.identity); } }

However I now get an error saying "collideEnemy.cogPrefab is never assigned to, and will always have its default value 'null". When I run the scene, the cog destroys fine but does not instantiate after the collision and gives me a "NullReferenceException" error. Any ideas?

avatar image whydoidoit · Nov 10, 2012 at 10:18 PM 0
Share

You don't need to delete the cogPrefab! Just remove that from your scene after you created the prefab.

You need to make cogPrefab variable in collideEnemy public and drag your cogPrefab from the project window onto the variable in the inspector.

avatar image Pixelen · Nov 11, 2012 at 08:32 PM 1
Share

Thanks, this worked perfectly (once I'd figured out what to do!). If anyone's trying to do the same thing this is what I did.

I put this after the $$anonymous$$onoBehaviour line:

public GameObject cogs;

Then inside my function and inside the if statement I put

Instantiate(cogs);

I realise this is basically the same code $$anonymous$$ike gave me, it just took me a while to understand it!

avatar image
1

Answer by FakeBerenger · Nov 10, 2012 at 10:05 PM

It's unecessary to destroy and instantiate an object whe all you want to do is show/hide. You can either play with the gameObject.active boolean (mind that the OnTriggerXXX functions won't be raised) or with the renderer.enabled one.

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

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

Destroying Objects. 1 Answer

Is there anyway to delete clones that are local variables? 1 Answer

References to GameObject become null 1 Answer

[Solved]Instantiated item won't get destroyed until "picked up" again. 1 Answer

[Solved] Unable to instantiate an object from within the OnTriggerEnter callback. 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