- Home /
Question by
choijaeyoung · Apr 25, 2020 at 09:51 PM ·
gameobjectscripting beginnergetcomponentscriptingbasicsorder-of-execution
What is the order of execution for the instantiated object's GetComponent.Script.DoSomething?
So, here's the simplified code for GUN.cs, which will shoot bullet prefab with PROJECTILE.cs:
GameObject bullet = Instantiate (prefab, transform.position, transform.rotation);
bullet.GetComponent<Projectile>().DoSomething();
In this case, when does DoSomething() of Projectile.cs occur? On Projectile.cs's Awake, OnEnable, or OnStart? Or on its first update?
Comment
Best Answer
Answer by yummy81 · Apr 26, 2020 at 05:32 AM
Replace your version of Projectile.cs with this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
public class Projectile : MonoBehaviour
{
void Awake() => Test();
void OnEnable() => Test();
void Start() => Test();
void Update() => Test();
public void DoSomething() => Test();
void Test(){
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
string methodName = methodBase.Name;
Debug.Log(methodName);
}
}
Your answer
Follow this Question
Related Questions
updating a gameobject variable from another script attached to another object 3 Answers
Access collider custom script's variables 1 Answer
How to Access Components in Children of GameObject? 1 Answer
how to access a float from one script in other script in different objects 3 Answers
Attacking mulitple targets simultanously 2 Answers