Please Help!

Updated on January 7, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
16 on January 5, 2017

http://imgur.com/a/6fPj1 in this picture you can see that all my scripts are valid, this appeared after i downloaded new unity version before there was no compile errors and i could play the game.
http://imgur.com/a/mSzxZ can someone tell me how can i fix this error i dont really understand what to do.

code:
using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour
{

public static GameMaster gm;

void Awake()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag(“GM”).GetComponent<GameMaster>();
}
}

public Transform playerPrefab;
public Transform spawnPoint;
public float spawnDelay = 2;
public Transform spawnPrefab;

public CameraShake cameraShake;

void Start()
{
if (cameraShake == null)
{
Debug.LogError(“No camera shake referenced in GameMaster”);
}
}

public IEnumerator _RespawnPlayer()
{
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(spawnDelay);

Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
GameObject clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
Destroy(clone, 3f);
}

public static void KillPlayer(Player player)
{
Destroy(player.gameObject);
gm.StartCoroutine(gm._RespawnPlayer());
}

public static void KillEnemy(Enemy enemy)
{
gm._KillEnemy(enemy);
}
public void _KillEnemy(Enemy _enemy)
{
GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}

}

it would be nice if you helped me !

  • Liked by
Reply
4 on January 5, 2017

Pro tip: Click on ‘Insert codes’ next to the ‘Insert images button when posting.
 
when instantiating with C#, instead of this:

GameObject clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;

try this:
GameObject clone = (GameObject) Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation);

Maybe it changed to this format after a unity update sometime, I’m not sure, but it’s what I use.
 

on January 6, 2017

Doesnt work still, only when i do this method
 

GameObject clone = Instantiate <GameObject> (spawnPrefab, spawnPoint.position, spawnPoint.rotation);

And this one is fixed too when i do the same
GameObject _clone = Instantiate <GameObject> (_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;

But then when in the game i respawn spawnPrefab doesnt appear :/ there must be other way

Devoted
on January 6, 2017

Don’t cast at all.
I don’t really understand why so many people call the object overload and then cast it… There is a generic overload that at compile time knows the type it returns, so no casting is needed.
It calls the generic overload automatically because of generic inference (when you input a parameter of a generic type and it knows the type without you putting the < and >).

Also, BinaryBeal, there are no difference in this case. The only difference is that when it fails to cast, it returns null, while the casting you told him to use will throw an exception. To clarify, this is how I instantiate:

Type clone = Instantiate(object, position, rotation);

Just make sure object is of type Type. Also, this is a template, so of course you need to replace it with what you need.
And I usually also add another parameter for the parent, but that’s not necessary.

Devoted
on January 6, 2017

That’s what I try to do too! Sometimes though, it doesn’t seem to work unless I force it (overload it?) with an type indicator. I swear, it’s just sometimes, and for seemingly no apparent reason…

Devoted
on January 6, 2017

That’s very weird, it works perfectly for me…

Show more replies
  • Liked by
Reply
Cancel
5 on January 6, 2017

But what’s the error message?

Also, a tip for life: Instead of using “FindObjectWithTag(tag).GetComponent<Component>()”, do “FindObjectOfType<Component>()”.
But in your case I would just do something like this:

gm = this;

It will run faster since it doesn’t go through all the game objects in the scene.

on January 6, 2017

http://imgur.com/a/8r7ei Here’s an error btw error comes from the script i downloaded from 2d brackeys.

Devoted
on January 6, 2017

spawnPrefab is of type Transform, so when you instantiate it, Instantiate returns Transform. You try to store it in a variable of type GameObject, which doesn’t make any sense, so you get conversion errors. You have three options:
1. Make spawnPrefab of type GameObject.
2. When instantiating, instantiate the game object, so do “Instantiate(spawnPrefab.gameObject)”, instead of “Instantiate(spawnPrefab)”.
3. Make “clone” of type Transform, and when destroying, do “Destroy(clone.gameObject)”, instead of “Destroy(clone)”.

It’s your choice. I personally would go with 1, if you ask me.

Devoted
on January 6, 2017

Responding to your initial tip Aviram, 
FindObjectOfType<TYPE>() works well when you only have one of those types in the scene, like a GameMaster, etc. but if not when you have multiple objects with similar scripts of different variables…
Just a thought.

Devoted
on January 6, 2017

But also FindGameObjectWithTag returns only one, so in that case they are the same.
I generally thing tags are a terrible t hing, to be honest. You should never trust string-based things like that, since chances are you will have typos and have no idea what’s going on, just too unsafe.

Devoted
on January 6, 2017

Fair enough.

Show more replies
  • Liked by
Reply
Cancel
4 on January 6, 2017

Alright, first of all, I think it’s better if you use the updated codes in your C#. So change each of the warnings with the line UnityEngine.Application.method_name to the correct SceneManager.method_name statement. Make sure you added the line using UnityEngine.SceneManagement at the top.

Now for the error. From what I tell, you’re using the wrong type for your Instantiate method. You’re trying to instantiate a spawnPrefab, and from my understanding, that’s a Transform type and not a GameObject.
So use this:

Transform clone  = (Transform)Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation);

Do that for your other error and it should work.

EDIT: I just checked Visual Studio for Unity 5.5 and it gave me a warning that a cast was not needed. So you might not have to add (Transform) or as Transform in your Instantiate method but if you’re not using the latest version or if there was an error, just add the cast like normal. Or Aviram’s code above should work.

on January 6, 2017

Thank you both so much! i used you method and it worked, thanks again!

Devoted
on January 6, 2017

Just like I said, it returns a transform on it’s own. Casting is like casting a Transform into a Transform, which is completely useless.

I still don’t get why everyone casts there… It’s just a thing so many people use and I seriously don’t understand why. It’s not like one or two people, it’s everyone (not almost everyone, but everyone). Is it like some new thing none knows about?

Devoted
on January 6, 2017

Good catch on realizing it was a Transform. I missed that.

on January 7, 2017

Replying to Or Aviram’s comment. I used casting because I used to watch a YouTube tutorial telling you that if you didn’t cast, it will show an error. The video did show an error so I just followed what they did. Honestly, I never tried the casting error myself and if I did, I’ve forgotten about it. So I was used to casting until recently, I found out that you don’t have to cast as it won’t return any error. I don’t know if this was a recent thing they changed and if it was there long ago, I was oblivious to it. Definitely a recent interesting find though.

Show more replies
  • Liked by
Reply
Cancel