hi,I am making a game and i am at the point that i have to copy a enemy game object. I have tried instantiate but it runs the code from the beginning again,is there any way to copy a game object and to make the program run from update? thx
this is my script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class moveenemy : MonoBehaviour {
public GameObject tower;
public Text towertext;
public Text LoseText;
private float towerx;
private float towerz;
private float distancex;
private float distancez;
private float health;
private float towerHealth;
private int createnewtime;
private GameObject New;
// Use this for initialization
void Start () {
towerx = tower.transform.position.x;
towerz = tower.transform.position.z;
distancex = towerx - transform.position.x;
distancez = towerz - transform.position.z;
health = 500;
towerHealth = 1000;
}
// Update is called once per frame
void Update () {
createnewtime = createnewtime + 1;
towertext.text = "Ally tower health: " + (int) towerHealth;
LoseText.text = "";
if(createnewtime == 300) {
New = Instantiate(gameObject);
New.transform.position = new Vector3(375,0.5f,400);
}
if (transform.position.x > towerx + 4) {
if (transform.position.z > towerz + Random.Range(1,4)) {
distancex = distancex + 1f;
distancez = distancez + 1f;
transform.position = new Vector3 (towerx - distancex, 0.5f, towerz - distancex);
}
}
if (towerx <= 200) {
if (towerx >= 0) {
if (towerz <= 200) {
if (towerz >= 0) {
health = health - 0.5f;
if(health <= 0) {
gameObject.SetActive (false);
}
towerHealth = towerHealth - 0.05f;
}
}
}
}
if (towerHealth <= 0) {
towerHealth = 0;
LoseText.text = "YOU LOSE!!";
Time.timeScale = 0;
}
}
}
What do you mean with, it runs the code from the beginning again? Maybe you could describe your problem more precisely
well,i got the tower health that i transform in the middle of the code, when i create the new game object it goes back to 1000
when you instantiate a gameobject the Awake and Start functions are called. Perhaps you can explain why you are trying to copy the tower so we can help you structure the gameobjects/code to handle it better. I’m guessing something like an tower upgrade or a model change to a damaged tower or something?
I’m also surprised line 42 and 43 aren’t throwing errors, New is a protected key word, don’t use it as a variable name.
no, i am trying to copy the enemy game object so i can have more than 1 enemy at a time and there aren’t errors because i put New instead of new… what i need is to make a copy of the enemy game object without the start function being called… is there anyway to do this or is there any function like start that lets this happen?
I would put a public function in your enemy script, and after instantiating, you could run the script or not. Like in your line 42:
if(createnewtime == 300) {
New = Instantiate(gameObject);
New.transform.position = new Vector3(375,0.5f,400);
New.GetComponent<YourScript>().RunStartScript();
}
So, instead of putting everything in start, you can just call the script as a “fake”-start function
ok thx!! another thing i could have done and i won’t do but anyway i want to know how to do is accessing a variable from a different script. Ive been seeing tutorials provided by the website and they are great
If the script is on the same GameObject, you access it by calling gameObject.GetComponent() which returns a reference to the script as a whole. You can add “.someMemberName” to the end to access a public member immediately, or assign the script reference to a variable of its own so you can access as many members as you want without reaching out and grabbing the other script over and over. For instance:
SomeScriptName scriptReference = gameObject.GetComponent<SomeScriptName>();
scriptReference.publicMember = someNewValue;
OR
gameObject.GetComponent<SomeScriptName>().publicMember = someNewValue;
If, however, you’re trying to access a script on a DIFFERENT GameObject, then you’ll need to start off by getting a reference to that other GameObejct- usually with GameObject.FindGameObjectWithTag() or by making a public GameObject variable for the current script and then dragging another GameObject onto it in the Inspector. Afterward, you can just call GetComponent() on it to get the script you need and everything else is the same.
thanks a lot this community is helping me learn very fast and well