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 RuneyWolf · Sep 12, 2013 at 02:57 AM · prefabcopy

Instantiated coconuts not working

Alright, so I created a palm_terrain object, then I created a coconut object. Both are properly tagged as "Palmtree" and "coconut". Now, my goal is to instantiate 3 coconuts at the tree position and have them fall as a test for what I'm doing in the future. The coconut is a prefab and the palm_terrain is as well.

The script worked perfectly before I went to bed, then I started messing with the Vector3 float values and it is completely broken. I've manipulated the values endless amounts of time and still to no avail. Anyone have any ideas?

 using UnityEngine;
 using System.Collections;
 
 public class CoconutGen : MonoBehaviour {
     
     public int numberOfCoconuts;
     public GameObject thisobject;
     public GameObject coconutobject;
     
     void Awake() {
         numberOfCoconuts = 0;
         GameObject thisobject = GameObject.FindGameObjectWithTag("Palmtree");
         GameObject coconutobject = GameObject.FindGameObjectWithTag("coconut");
     }
     
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         while (numberOfCoconuts < 3){
             Instantiate(coconutobject, thisobject.transform.position + new Vector3(5f, 5f, 5f), Quaternion.identity);
             numberOfCoconuts ++;
         }
     }
 }

Thanks for looking at this, all answers are appreciated.

Comment
Add comment · Show 8
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 whydoidoit · Sep 12, 2013 at 03:05 AM 1
Share

So if Coconut is a prefab being instantiated, you should be assigning it in the inspector not finding it in the scene like you are in Awake...

avatar image vexe · Sep 12, 2013 at 03:58 AM 0
Share

Aside from that, you have other problems in your code. You have a global thisobject and coconutobject, then in your Awake, your are declaring two new local variables with the same names. Once Awake finishes execution, the two local references will be lost. Then, accessing thisobject and coconutobject should give you back null cuz you didn't assign them anything, you assigned the local ones and not the global. - Correct your awake:

  void Awake() {
    numberOfCoconuts = 0;
    thisobject = GameObject.FindGameObjectWithTag("Palmtree");
    coconutobject = GameObject.FindGameObjectWithTag("coconut");
 }

Better yet, do what @whydoidoit told you, assign your stuff via the inspector if you can.

avatar image RuneyWolf · Sep 12, 2013 at 08:22 PM 0
Share

I'm fairly certain I did what you both asked me but it hasn't changed anything. I removed the Awake function and the local variables that vexe pointed at. I then assigned the GameObjects through the inspector and it still isn't working. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class CoconutGen : $$anonymous$$onoBehaviour {
     
     public int numberOfCoconuts;
     public int maxAmountOfCoconuts;
     public GameObject thisobject;
     public GameObject coconutobject;
     
 
     // Use this for initialization
     void Start () {
         numberOfCoconuts = 0;
         maxAmountOfCoconuts = 3;
     }
     
     // Update is called once per frame
     void Update () {
         while (numberOfCoconuts < maxAmountOfCoconuts){
             Instantiate(coconutobject, thisobject.transform.position + new Vector3(0, 0, 5f), Quaternion.identity);
             numberOfCoconuts ++;
         }
     }
 }
avatar image Hoeloe · Sep 12, 2013 at 08:34 PM 0
Share

Exactly what is it doing, and what are you expecting it to do?

avatar image RuneyWolf · Sep 12, 2013 at 10:19 PM 0
Share

Yeah, I decided to change my setting to a desert so there won't be anymore need for coconuts. Thanks for the help.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

18 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 avatar image

Related Questions

Awake() not called on script referenced directly from Project window 1 Answer

Can you copy a Prefab? 4 Answers

Prefab connection problem 1 Answer

Any way to copy and paste GameObjects programatically? 1 Answer

Instantiate: Create Connection 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