Unity C# if, else if, else Error :( Please Help
Hello, i want that if my rigidbody collide with some tagged colliders than it spawn the particle system for the tag- tag wood-wood particles this is my code:
using UnityEngine;
using System.Collections;
public class ImpactScanner : MonoBehaviour {
public GameObject MetalParticles;
public GameObject WoodParticles;
public GameObject DirtParticles;
public GameObject BodyImpact;
public float DestroyTime;
void Start () {
Destroy(gameObject, DestroyTime);
}
void OnCollisionEnter(Collision collision) {
if (other.CompareTag("Metal")) {
MetalParticles instantiatedProjectile = Instantiate(projectile,transform.position,transform.roation) as GameObject;
}
if (other.CompareTag("Wood")) {
WoodParticles instantiatedProjectile = Instantiate(projectile,transform.position,transform.roation) as GameObject;
}
if (other.CompareTag("Dirt")) {
DirtParticles instantiatedProjectile = Instantiate(projectile,transform.position,transform.roation) as GameObject;
}
if (other.CompareTag("Player")) {
BodyImpact instantiatedProjectile = Instantiate(projectile,transform.position,transform.roation) as GameObject;
}
}
}
but i always get this error :( Assets/FPS REALISM PRO KIT/KerboGames/Scripts/Projectile/ImpactScanner.cs(30,33): error CS0118: ImpactScanner.WoodParticles' is a
field' but a `type' was expected and this wich every particles
Have a nice Day Regards
Where is "projectile" declared? Also, those need to be GameObject instances. You are using a variable name as a class, hence your error?
So
$$anonymous$$etalParticles instantiatedProjectile = Instantiate(projectile,transform.position,transform.roation) as GameObject;
should be just
$$anonymous$$etalParticles = Instantiate(projectile,transform.position,transform.roation) as GameObject;
Also, you should do as you claim to do in the question title: use the else with your ifs
if (other.CompareTag("Wood")) {
}
else if (other.CompareTag("Dirt")) {
There's no need to check if the tag is "Dirt" if you already discovered it was "Wood"
@12ttttt If you're asking something make sure you always end the topic by marking an answer correct.
Answer by Anton-Korhonen · Nov 02, 2016 at 06:22 PM
Like the error says, WoodParticles is not a type, but a field. You will get this error with all of your code lines with Instantiate in the ImpactScanner.cs script.
You have to leave the instantiatedProjectile out of the function call:
WoodParticles = Instantiate(projectile,transform.position,transform.roation) as GameObject;
And use else if as there is no need to check all of the objects if one is already found.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095836im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Error CS0535? 0 Answers
Roll a Ball script error?! 2 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Scene Won't Load 1 Answer
Socket: bind failed, error 3 Answers