I’ve been working on a rhythm game where you instantiate the objects you interact with. Like you have to press a certain key whenever the circle gets to the line. But whenever I instantiate my prefab, the script is the same through all instantiations. Is there some way I can instantiate a script?
In what way - do you mean - is the script component the same each time you instantiate a copy of your prefab? When you instantiate a prefab, any components on the instance will also be instances of your original components, including any script components you wrote.
You can of course add instances of a script component onto a GameObject at runtime with AddComponent().
Is there some sort of weird behaviour you’re experiencing to warrant this question?
What I want to do is create a copy of the script with the prefab. I don’t want the same script on every instantiated object. I want a copy of it.
If you have a prefab and you instantiate it, then the components attached to it are also instances of that script. If you’re using static variables though, those belong to the class and not the instances, so those values aren’t going to be unique. I think the question is, why do you think it’s the same script?
So what I’m doing is making a torus slowly scale down till it’s gone. Whenever I instantiate another one after the other one is scaled down it’s also gone because it as well was scaled down to 0. What I want is of course is for it to reset to it’s original position than it be the same as the other one. One thing that may be wrong is I could be using the same torus but I don’t think that’s it since I am instantiating another copy.
Show us your code.
Both the class/script that’s on the prefab, and the code that clones the prefab.
As @Brathnann said, when you instantiate a prefab, a clone of the gameobject’s and all components on it are created. They already are new instances. So if you’re having some weird behaviour… it’s coming from something else going on in your code.
So I figured out the problem. I’m accessing the same torus with all the instantiated prefabs. So now what I want is for the script to access its own torus that it instantiated.
Is that really so difficult to understand Jerald? How do you expect anyone to help without that? We can’t know what’s going on with your code when we can’t see it. What exactly do you expect from us? And how should we provide this help when you refuse to show your code?
Sorry, I don’t usually do this stuff at home. I usually go on forums when I’m at school or something like that. Also, I found out the problem already, it just turns out I’m a massive idiot.