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 Ochreous · Nov 12, 2013 at 01:31 AM · c#instantiatecollider

C# Instantiating a Gameobject with a Flat Sphere Collider

Is it possible to create a gameobject with a collider upon instantiation? I want to Instantiate a gameobject that only has a flat sphere Collider but I don't want create it as a prefab and then instantiate it because I want to create the scale upon instantiation. I'm unsure how to instantiate a gameobject and then add a flat sphere collider to it. Would you create the gameobject as a variable and then use it?

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 clunk47 · Nov 12, 2013 at 01:57 AM

 using UnityEngine;
 using System.Collections;
 
 public class CreateNewGO : MonoBehaviour 
 {
     GameObject go;
     GameObject meshObject;
     
     void Awake()
     {
         go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         go.transform.position = transform.position + (transform.forward * 5)+(-transform.up);
         Destroy (go.GetComponent<SphereCollider>());
         
         //Change mesh of go. Will load object from Resources/Prefabs/Put name of your prefab here.
         meshObject = (GameObject)Resources.Load ("Prefabs/NameOfYourPrefab");
         go.GetComponent<MeshFilter>().mesh = (Mesh)Instantiate(meshObject.GetComponent<MeshFilter>().mesh);
         
         go.transform.localScale = new Vector3(3, 3, 3);
         go.AddComponent<MeshCollider>();
         
         
         //Test collider with rigidbody
         GameObject test = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         test.transform.localScale = new Vector3(.3f, .3f, .3f);
         test.transform.position = transform.position + Vector3.forward * 5;
         test.AddComponent<ConstantForce>().force = Vector3.right;
     }
 }
 
Comment
Add comment · Show 6 · 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 Ochreous · Nov 12, 2013 at 03:52 AM 0
Share

How do you change what the mesh collider's mesh in C#?I know you can change what it is directly on the gameobject via the inspector but I don't know how to change it in a C# script. I only want the flat sphere collider. No mesh renderer or filter. I really don't want to do that by creating a primtiveType.Sphere and then destroying it's components.

avatar image clunk47 · Nov 12, 2013 at 04:10 AM 0
Share

Edited answer to include changing mesh at runtime.

avatar image Ochreous · Nov 12, 2013 at 04:44 AM 0
Share

In order to get the kind of collider I'm looking for am I going to need to create a prefab? I was trying to avoid that.

avatar image clunk47 · Nov 12, 2013 at 05:01 AM 1
Share

Well you need to have a mesh to go off of.... Otherwise you need to write your vertices at runtime... I think I don't fully understand exactly what you're trying to do..

avatar image clunk47 · Nov 12, 2013 at 05:23 AM 1
Share

Yeah just use

 go.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
Show more comments

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

Null Reference Exception on instantiated object's script. 2 Answers

C# Check If Gameobject is within Collider 1 Answer

How do i Instantiate a prefab with specific assests included 3 Answers

How to Name Individual Buttons Within a List 2 Answers

Terrain with lots of objects 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