- Home /
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.
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...
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.
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 ++;
}
}
}
Exactly what is it doing, and what are you expecting it to do?
Yeah, I decided to change my setting to a desert so there won't be anymore need for coconuts. Thanks for the help.
Your answer
Follow this Question
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