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
1
Question by PokeJoe · Dec 12, 2013 at 10:16 AM · spawnenumscope

Can I call a function on a behavior attached to a prefab from it's spawner object?

I am making a falling block game. The blocks can be red, orange, yellow, green, or blue. Besides just having different materials, each color has other properties I'll need to implement later.

I also want it to be possible for other objects to change the color of each individual instance of Box later on. I'm not sure if this is formed correctly, but I don't get any errors with this code:

 public class BoxProperties : MonoBehaviour {
 
     public Material RedMat;
     public Material OrangeMat;
     public Material YellowMat;
     public Material GreenMat;
     public Material BlueMat;
 
     public enum BoxType {        Red, Orange, Yellow, Green, Blue    }
 
 
     public BoxType myType;
 
     ////snipped Start() and Update()
 
     public void setType(BoxType newtype) {
 
         switch (newtype) {
             case BoxType.Red:
                 renderer.material = RedMat;
                 break;
             case BoxType.Orange:
                 renderer.material = OrangeMat;
                 break;
             case BoxType.Yellow:
                 renderer.material = YellowMat;
                 break;
             case BoxType.Green:
                 renderer.material = GreenMat;
                 break;
             case BoxType.Blue:
                 renderer.material = BlueMat;
                 break;
         }
 
         myType = newtype;
     }
 }



I made setType() public so other objects can use it to change the type of any given instance.

Additionally, I have a non-renderable cube that represents a spawn volume. I am able to get the Box instances to spawn at random points in the cube and fall, but I am not sure how I can get at the prefab instance's setType method to set the type of box after spawning.

 public class SpawnBoxes : MonoBehaviour {
     private Vector3 nextpos;    //Randomly picked new spawn position
     private float collecttime;    //Counts how long has passed since the last spawn
     private GameObject nextbox;
 
     public float spawntime;
     public GameObject spawnee;    //Box object goes here
 
     // Use this for initialization
     void Start () {
         collecttime = 0;
     }
     
     // Update is called once per frame
     void Update () {
         collecttime += Time.deltaTime;
 
         if (collecttime > spawntime) {
             nextpos = randPos(renderer.bounds.size, transform.position);
             collecttime = 0;
             nextbox = Instantiate(spawnee,nextpos,Random.rotation) as GameObject;
             //Set type of nextbox, not sure how to access that function, but it's part of a component script attached to the Box prefab
             nextbox.GetComponent("BoxProperties").setType(BoxType.Green); //Doesn't work.
         }
     }
 //snipped randPos()
 
 }


I realize there are some other approaches, like making separate prefabs for each Box type and spawning one randomly, but I wonder if there is a way to make this call work?

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 KellyThomas · Dec 12, 2013 at 10:22 AM

SomeScriptName someScript = someGameObject.GetComponent();

someScript.SomeMethod();

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 PokeJoe · Dec 12, 2013 at 11:13 AM 1
Share

Thanks, this answers the question in the abstract but it took a little finessing to get it to work in context - but it pointed me in the right direction. In particular, an explicit typecast is necessary.

For posterity, my code now reads:

 BoxProperties props = spawnee.GetComponent("BoxProperties") as BoxProperties;
 props.setType(BoxProperties.BoxType.Yellow);
avatar image KellyThomas · Dec 12, 2013 at 11:33 AM 0
Share

Sorry for the dodgy answer (I was a but distracted and entered the wrong form.

$$anonymous$$y preferred form is:

 SomeScriptName someScript = someGameObject.GetComponent<SomeScriptName>();

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

17 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

Related Questions

JS: Dump array value in an Enum 1 Answer

How To Random Range a Float? 2 Answers

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers

How can I use raycasthit and collider raycast to hit only if the mouse position is over a specific object ? 1 Answer

getting literal value for enum 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