- Home /
How to make object visible without instantiating it?
Hello! I am making a building system to place turrets in my top down shooter but my problem is that when someone has selected a type of turret but decides to change the turret, when they select the new type of turret the previous turret is built. The problem I realized that I had was that I was already instantiating the object before placing it. What I need to do is make the object visible before placing without instantiating it so that when I change the type of turret it wont just be placed in a random place. Another possible solution is that if my mouse is over a UI I set TurretChoosen to false. The problem is that I don't know how to check if my mouse is over UI.
Placing Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointClickPlace : MonoBehaviour {
public Transform currentTurretPreview;
public Transform currentTurret;
public Camera MainCamera;
private OpenBulletChooser TurretChooser;
public bool TurretChoosen;
public int TypeTurretChoosen;
// Use this for initialization
void Start () {
TurretChooser = GetComponent<OpenBulletChooser>();
}
// Update is called once per frame
void Update () {
if (TurretChoosen == true)
{
Vector3 mPosition = Input.mousePosition;
mPosition = new Vector3(mPosition.x, mPosition.y, transform.position.y);
Vector3 Camera = MainCamera.ScreenToWorldPoint(mPosition);
currentTurretPreview.position = new Vector3(Camera.x, 1, Camera.z);
if (Input.GetMouseButtonDown(1) && TurretChoosen == true)
{
Instantiate(currentTurret, new Vector3(Camera.x, 1, Camera.z), currentTurret.rotation);
TurretChoosen = false;
}
}
}
public void SetTurret(GameObject Turret, GameObject TurretPrev)
{
currentTurretPreview = ((GameObject)Instantiate(TurretPrev)).transform;
currentTurret = Turret.transform;
TurretChoosen = true;
}
}
To render something without instantiation you can use Graphics.Draw$$anonymous$$esh
PS: don't let $$anonymous$$atrix4x4 scare you, use $$anonymous$$atrix4x4.TRS(pos,rot,scale) to create it.
Can you give me an example of it for the (currentTurretPreview = ((GameObject)Instantiate(TurretPrev)).transform;) script? Like change it to a Draw$$anonymous$$esh to whatever TurretPrev Gameobject is.Sorry its just I have never used this before and I dont understand the unity learn page.
Answer by JonPQ · Oct 24, 2017 at 10:47 PM
if there is only one Renderer / mesh you can enable or disable the renderer component. GetComponent(Renderer).enabled = true; //or false but you should cache this in a variable. avoid using GetComponent when possible.
Or gameObject.SetActiveInHierarchy( true)
Or you use pooling... create a bunch of objects at level start that you will need later, and turn them all off, with setActiveInHierarchy.... then grab them from the list and enable them again when you need them. Pooling is much faster than instantiating.
I would recommend encapsulating whether you are instantiating or pooling.
Then again, I would recommend encapsulating everything.
There are also lots of good Pooling assets on the asset store if you don't feel like writing one.
Answer by heybaw · Oct 25, 2017 at 12:30 AM
To add to @JonPQ's answer, I would definitely recommend that you use an object pool. Give each turret a 'placed' flag to make things easy for yourself, and then place them somewhere in your game, in List objects. Every time the user seems like he might use that turret, you search for the first one that doesn't have a 'placed' flag. If the user clicks anywhere, without that flag changing, you return the object back to the pool. Once the pool reaches one, you replenish it.
I'm not sure how clear this answer is, so if you are having trouble, look up "Object Pooling" on codeproject, until you find the tutorial that speaks to you.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
check If Building will collide with something before placed? 1 Answer
Question about Instantiation 1 Answer
How do you destroy a GameObject during the build process? 1 Answer