- Home /
How to Instantiate prefab as child? (Java)
I am trying to Instantiate prefab (weapon) on a transform. This is script I found online but it doesn't work giving error:
var Weapon1 = Instantiate(GunType, Weapon1Spawn.transform.localPosition, Weapon1Spawn.transform.localRotation);
Weapon1.transform.parent = Weapon1Spawn.transform;
Error I get with that:
Quick note: Java is not JavaScript. To cite a popular comparison
Java is to JavaScript as ham is to hamster.
Unity does not support Java as a program$$anonymous$$g language.
I know it doesn't support java itself but it's still javascript and I use java as shortening. I Don't think anybody uses actual meaning of java on Unity forum
I do not think that it is a good idea to use a shortening that also means something different in the same field. Particularly when you can use the common abbreviation JS. If you take a look at the Java topics, you can see that there are many questions in this forum that actually are about Java. That is because Unity is often used for Android applications (which use Java) and even has an interface for Java (JNI = Java Native Interface) with classes that literally contain the word Java (AndroidJavaObject, AndroidJavaClass or AndroidJavaProxy).
Answer by joaosilver11 · Oct 05, 2016 at 11:42 AM
When you instantiate the 4th argument is the parent, for example
Instantiate (prefab, position, rotation, parent transform);
in your case, that would be:
var Weapon1 = Instantiate(GunType, Weapon1Spawn.transform.localPosition, Weapon1Spawn.transform.localRotation, Weapon1Spawn.transform;);
Thank you, I think that moved me forward but now I am getting another error and I think it wants me to tell the local position in Vector3 and rotation on Quaternion. Would you know how to get these from local space? Error:
Your answer
Follow this Question
Related Questions
Problems instantiating a prefab (javascript) 1 Answer
Instantiating from an object that's in an array 1 Answer
Simple Instantiate gameObject problem 3 Answers
Parenting GameObjects 1 Answer
iOS Instantiate transform as child -- positioning bug 2 Answers