Creating When Needed - Challenge?

Hi all,

I haven’t put any thought into this challenge yet, I blew out my brain with the last challenge with the buttons, what I did was nowhere near as simple as Ben’s way, though, something is still nagging at me, but, it seems to work just fine.

Anyway, with this challenge I am thinking that we will need to use a “find” method, I don’t know yet, perhaps a just a simple Find or objectoftype or tag (something) like that - but, have been told to avoid using anything with Find on the Unity forums when I sometimes ask question about finding other scripts or Gameobjects and whatnot or in one case finding a script in another scene - can’t remember why now.

Anyone have any thoughts on this matter? Are there alternate ways?

Regards,
Vaughan.

Hi Vaughan,

I don’t recall the specific challenge, but with regards to GameObject.Find, it’s basically an expensive method, on performance. In variably you may need to use Find and also GetComponent, but where possible you should aim to reduce the amount of times this is necessary. As an example, perhaps you need to find a LevelManager in your scene, you could do this perhaps once as the scene loads and then hold a reference to it, rather than Find it lots of times in different scripts.

Scrutinise anything where there is repetition :slight_smile:

1 Like

Right so using the various types of finds in things in Start Awake methods isn’t that taxing but using them inside For, ForEach and Update methods and inside methods that are pinging back and forth is.

Tuigum.

I was a little disappointed with myself not getting the second challenge done. I thought the code I had would work:

    obj = GameObject.Find("Projecti");

    if(obj==null)
    {
        print("ShootingProjectiles: we hav a problem....");
                 
        obj = new GameObject("Projecti");
        projectile_parent = obj;
    }

But it kept on throwing an error later in the script when the projectile was actually fired and trying to be parented to the object

NullReferenceException: Object reference not set to an instance of an object

Can’t figure out why that didn’t work. Certainly not as streamlined as @ben’s solution but thought I had that one :confused:.

Hey Vaughan,

That’s right, where-ever possible, if you need something more than once, cache it and re-use.

With regards to the challenge, I would probably need to see the full script for me to be of much use.

1 Like

Not that it’s really necessary, but, this is the whole code I am using for the shooting of Projectiles. Please excuse the extreme use of verbose comments. It help me organise my thoughts, code and why I’m doing what I’m doing (mostly), what worked and what didn’t. So I can quickly change between code when making changes.

I’ve accepted that Ben’s solution is better, but, I’d like to know why mine didn’t. But if you don’t want to look over it that’s fine. I’ll just have to live with the fact that I am not always right :scream: :rofl:.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shootingProjectiles : MonoBehaviour {

    //get access to the game object they will be shooting
    //across the scene using the "actual" prefabs
    //set in inspector - call it bullet.
    public GameObject bullet;

    //nope never parented this in the heirarchy
    //before this will be ... different
    //public GameObject projectile_parent;
    private GameObject projectile_parent;

    //Also need a link to the origin we are shooting from
    public Transform projectile_origin;

    //using this as a temporary store code in start
    private GameObject obj;

    //do stuff here
    public void Start()
    {
        //without this my code throws an error
        //projectile_parent = new GameObject();

        //Ben's code
        //find whether the Projectiles gameobject exists
        //projectile_parent = GameObject.Find("Projecti");
        //mine won't???
        obj = GameObject.Find("Projecti");

        //ben's code fine
        //if(projectile_parent == null)
        //mine - not so fine
        if(obj==null)
        {
            print("ShootingProjectiles: we huv a problemo....");
            //below works
            //projectile_parent = new GameObject("Projecti");

            //mine won't work
            obj = new GameObject("Projecti");
            projectile_parent = obj;
        }
        else
        {
            print("the gameObject Shooting projectiles exists...yay!!!");
        }
       
    }


    private void Fire_Projectile()
    {
        GameObject bullet_clone;

        //get possy of the origin point we are firing from
        //not really needed but I remember doing this before
        Vector3 possy = projectile_origin.position;

        //instantiate the projectiles call them clones
        //seems fairly standard practice
        bullet_clone = Instantiate(bullet, possy, transform.rotation) as GameObject;

        //parent object to Projectile for some reason I don't understand
        //oh I get it - using it to send it to the projectiles section in Heirarchy
        bullet_clone.transform.parent = projectile_parent.transform;
        //problem is HERE!!!!!!!!!!!!!!!!!!!!!!

        //what is calling the firing method?
        //this is done in the cactus/Gnome isAttacking animation - cooool.
    }//end Fire_Projectiles

}//end of class shootingProjectile

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms