- Home /
[SOLVED] Only instantiating once
So, I have some code that I am trying to use to create a line over the top of another line (a gameobject), however, that line underneath is made from a prefab. I have the original prefab to the side, but whenever I try to instantiate a line, it only works once, even though I can click on different ones, and also it only pastes it over the prefab to the side. Any ideas?
Code:
var newSkin : GUISkin;
var redLine : GameObject;
var blueLine : GameObject;
var x : int;
var y : int;
var usable : boolean;
var cube : GameObject;
var isHit : boolean;
function Start() {
x = transform.position.x;
y = transform.position.y;
usable = true;
}
function Update() {
lineButton();
if (Input.GetMouseButton(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
if (hit.collider.gameObject.tag==gameObject.collider)
{
isHit = true;
}
else
{
isHit = false;
}
}
}
lineButton();
}
function lineButton() {
if(!VarDefiner.isMainMenu)
{
if(isHit && usable)
{
if(VarDefiner.redTurn)
{
cube = Instantiate(redLine, new Vector3(transform.position.x, transform.position.y, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 89.2929));
usable = false;
isHit = false;
VarDefiner.redTurn = false;
}
else
{
cube = Instantiate(blueLine, new Vector3(x - 0.5, y + 0.35, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 89.2929));
usable = false;
isHit = false;
VarDefiner.redTurn = true;
}
}
}
}
You had a different expectation? You're instantiating the the prefab into the same cube variable with the same values for placement. When you keep calling lineButton() it is actually doing the same thign over and over. $$anonymous$$ake List. and add the instantiated objection into that. That way you are keeping each instance of the line, not over writing one that exists in memory
I thought it would create a new one for every GameObject it was attached to... If it won't, how would I fix this?
@SpaceFiveFive, you didn't state that in the question, i could only assume. Yes if you have a script, attached to a bunch of objects and a prefab attached to those scripts in the inspector and you run the script and EACH script dynamically creates and instantiates the prefab, they "SHOULD" be their own copy. If you are dynamically setting the positions and such, whether it's getting it from the parent gameobject or some other scheme, you should be able to see them
Sorry about that. The only issue is that they aren't their own copy.
@SpaceFiveFive, there isn't enough information, post some screen shots of the scene setup with the scripts attached. Explain the interaction those scripts and the user, this will better help the viewer understand and come up with idea's.
Answer by MrSoad · Oct 13, 2014 at 06:58 PM
Have you considered using Unity's "LineRenderer" for your lines. You can give it the start and end point(your dot coordinates) and it will draw a line between the two. You can change how it looks via its Material.
Should be a lot easier than messing around with pre-made lines and prefabs for what it looks like you are doing.
Take a look at this post : http://answers.unity3d.com/questions/470943/collider-for-line-renderer.html For how to do that. i don't see any reason why you cannot use a different collider to the one used here, maybe a 2D box collider rather than a 3D capsule collider.
However, I'd like to know why it only creates one, because everything else works.
Your answer
Follow this Question
Related Questions
Create clickable GameObjects (JS) 1 Answer
How to activate a button? 1 Answer
[Logical Error] Select a game object and perform actions using GUI.Button 1 Answer
Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer
Changing variable in a script of a game object hit with raycast 1 Answer