- Home /
Question by
davidragge · May 17, 2015 at 11:23 AM ·
instantiateobjectfollow
Make instantiated object follow player or another object
I'm trying to make an instantiated object to follow my itemSpawn object. But when i click in an area the object gets instantiated but i doesn't follow my itemSpawn.
Main code thing:
using UnityEngine;
using System.Collections;
public class Grabitem : MonoBehaviour {
public GameObject greenLiquid;
public GameObject blueLiquid;
public Transform itemSpawn;
public GameObject player;
public static bool holdGreenLiquid;
public static bool holdBlueLiquid;
public static bool destroyItem;
private bool used;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
itemSpawn = GameObject.Find("Player").transform;
if (holdGreenLiquid) {
if (!used) {
Instantiate (greenLiquid, itemSpawn.transform.position, itemSpawn.transform.rotation);
Debug.Log("Should Instantiate");
destroyItem = false;
used = true;
}
}
if (!holdGreenLiquid) {
destroyItem = true;
}
}
}
Code for checking collision and mouse input on the object:
using UnityEngine;
using System.Collections;
public class GreenLiquid : MonoBehaviour {
void OnMouseDown () {
if (Input.GetButtonDown("Fire1")) {
Grabitem.holdGreenLiquid = true;
Debug.Log("Working");
}
}
}
Destroy the object if it is used:
using UnityEngine;
using System.Collections;
public class Destroy : MonoBehaviour {
public Transform position;
void Update () {
if (Grabitem.destroyItem == true) {
Destroy(transform);
}
}
}
Any help is appreciated :)
Comment
Your answer
Follow this Question
Related Questions
Lock rotation of an instantiated object to another 1 Answer
Everytime I Instantiate, object becomes smaller. 2 Answers
Have a Timer Counts To 5 sec. Then Make a Sphere Appear While Using Instantiate 1 Answer
Object instantiation Logic Javascript. 1 Answer
How do I instantiate certain objects to appear in a specific spot? 3 Answers